-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
153 lines (140 loc) · 4.9 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using MoreLinq;
using Newtonsoft.Json.Linq;
namespace SEApiComposeImages
{
class User
{
public int Id;
public BitmapFrame Image;
public Uri ImageUri;
public string DisplayName;
}
class Program
{
static void Main(string[] args) => new Program().Run();
Random random = new Random();
const string FileName = "TopUserIds.txt";
void Run()
{
var topids = GetIds(FileName);
var topUsers = GetUsers(topids);
int cols = 22, rows = 6;
int count = 3;
var images = topUsers.Select(u => u.Image).Take(cols * rows).ToList();
for (int i = 0; i < count; ++i)
{
var shuffledImages = Shuffle(images);
var result = ImageTools.CombineImages(shuffledImages, GetGridSettings(cols, rows));
ImageTools.SaveImageAsPng(result, $@"combined-{cols}x{rows}-{i}.png");
}
}
IEnumerable<int> GetIds(string filename)
{
return File.ReadLines(filename).Distinct().Select(int.Parse);
}
IEnumerable<User> GetUsers(IEnumerable<int> ids)
{
const int batchSize = 50;
int i = -1;
foreach (var idBatch in ids.Batch(batchSize))
{
var userBatch = FetchUsers(idBatch);
foreach (var user in userBatch)
{
i++;
var uri = user.ImageUri;
Console.WriteLine($"Downloading image #{i}, user={user.DisplayName}, id={user.Id}");
var image = DownloadImageByUri(uri);
if (ImageTools.IsAutomaticImage(image))
{
Console.WriteLine($"Image is auto, skipping");
continue;
}
user.Image = image;
yield return user;
}
}
}
WebClient PrepareWebClient() => new GzipHttpWebClient() { Encoding = Encoding.UTF8 };
IEnumerable<User> FetchUsers(IEnumerable<int> ids)
{
var idsList = ids.ToList();
var queryUri = BuildQuery(idsList);
string json;
using (var cl = PrepareWebClient())
json = cl.DownloadString(queryUri);
JObject total = JObject.Parse(json);
JArray items = (JArray)total["items"];
var users = items.Select(JsonToUser).ToDictionary(u => u.Id);
return idsList.Select(id => users[id]);
}
User JsonToUser(JToken juser)
{
var displayName = (string)juser["display_name"];
var userId = (int)juser["user_id"];
var imageLink = (string)juser["profile_image"];
return new User() { Id = userId, DisplayName = displayName, ImageUri = new Uri(imageLink) };
}
Uri BuildQuery(List<int> ids)
{
if (ids.Count > 100)
throw new ArgumentException("Batch size too big");
var idsCombined = string.Join(";", ids);
var idsCombinedEncoded = WebUtility.HtmlEncode(idsCombined);
var pattern = $"https://api.stackexchange.com/2.2/users/{idsCombinedEncoded}?page=1&pagesize={ids.Count}&site=ru.stackoverflow";
return new Uri(pattern);
}
BitmapFrame DownloadImageByUri(Uri uri)
{
var ms = new MemoryStream();
using (var client = PrepareWebClient())
using (var ns = client.OpenRead(uri))
ns.CopyTo(ms);
ms.Position = 0;
var frame = BitmapFrame.Create(ms, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
frame.Freeze();
frame.Metadata.Freeze();
return frame;
}
List<T> Shuffle<T>(IEnumerable<T> seq)
{
var result = new List<T>();
foreach (var s in seq)
{
int j = random.Next(result.Count + 1);
if (j == result.Count)
{
result.Add(s);
}
else
{
result.Add(result[j]);
result[j] = s;
}
}
return result;
}
ImageGridSettings GetGridSettings(int cols, int rows)
{
return new ImageGridSettings
{
Columns = cols,
Rows = rows,
CellPixelWidth = 128,
CellPixelHeight = 128,
Gap = 5,
CornerRadiusX = 10,
CornerRadiusY = 10,
BackgroundColor = Colors.Black
};
}
}
}