-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrizeDraw.linq
171 lines (140 loc) · 4.58 KB
/
PrizeDraw.linq
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<Query Kind="Program">
<Reference><RuntimeDirectory>\System.Net.Http.dll</Reference>
<NuGetReference>Newtonsoft.Json</NuGetReference>
<Namespace>System.Net.Http</Namespace>
<Namespace>System.Threading.Tasks</Namespace>
<Namespace>Newtonsoft.Json</Namespace>
</Query>
public const string _meetupDotComEventId = "241048316";
async Task Main()
{
// Get list of attendees
var attendees = await GetAttendees();
// Add completely pointless delay to add a bit of suspense!
await RunPointlessProgressBar();
// Randomize a winner!
var winner = PickRandomAttendee(attendees);
// Write winner's details to a file
File.AppendAllText(
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "LINQPadPrizeDrawWinner.txt"),
JsonConvert.SerializeObject(winner, Newtonsoft.Json.Formatting.Indented));
// Dump the winner to the output window
new
{
winner.Name,
ProfileUri = new Hyperlinq ($"https://www.meetup.com/dotnetoxford/members/{winner.AttendeeId}/", "Profile Page"),
Image = Util.Image(winner.ImageUri) ?? Util.Image("https://tinyurl.com/ybh4jwmm"),
}.Dump();
}
private async Task<AttendeeModel[]> GetAttendees()
{
using (var client = new HttpClient())
{
var eventId = _meetupDotComEventId;
client.BaseAddress = new Uri("https://api.meetup.com");
var response = await client.GetAsync($"/dotnetoxford/events/{eventId}/rsvps");
response.EnsureSuccessStatusCode();
var content = await response.Content.ReadAsStringAsync();
var rsvps = JsonConvert.DeserializeObject<List<MeetupComResponseModel>>(content);
return rsvps.Where(x => x.response == "yes").Select(x => new AttendeeModel
{
Name = x.member.name,
AttendeeId = x.member.id,
ImageUri = x.member.photo?.highres_link ?? x.member.photo?.photo_link,
}).ToArray();
}
}
private async Task RunPointlessProgressBar()
{
var progressBar = new Util.ProgressBar(true);
progressBar.Dump();
for (int i = 1; i <= 100; i++)
{
await Task.Delay(50);
progressBar.Percent = i;
}
}
private AttendeeModel PickRandomAttendee(AttendeeModel[] attendees)
{
var rand = new Random();
return attendees[rand.Next(0, attendees.Count())];
}
public class AttendeeModel
{
public string Name { get; set; }
public int AttendeeId { get; set; }
public string ImageUri { get; set; }
public string Image => ImageUri ?? @"Images\NoPhoto.png";
}
public class MeetupComResponseModel
{
public long created { get; set; }
public long updated { get; set; }
public string response { get; set; }
public int guests { get; set; }
public Event _event { get; set; }
public Group group { get; set; }
public Member member { get; set; }
public Venue venue { get; set; }
}
public class Event
{
public string id { get; set; }
public string name { get; set; }
public int yes_rsvp_count { get; set; }
public long time { get; set; }
public int utc_offset { get; set; }
}
public class Group
{
public int id { get; set; }
public string urlname { get; set; }
public string name { get; set; }
public string who { get; set; }
public int members { get; set; }
public string join_mode { get; set; }
public Group_Photo group_photo { get; set; }
}
public class Group_Photo
{
public int id { get; set; }
public string highres_link { get; set; }
public string photo_link { get; set; }
public string thumb_link { get; set; }
public string type { get; set; }
public string base_url { get; set; }
}
public class Member
{
public int id { get; set; }
public string name { get; set; }
public Photo photo { get; set; }
public Event_Context event_context { get; set; }
public string role { get; set; }
public string bio { get; set; }
}
public class Photo
{
public int id { get; set; }
public string highres_link { get; set; }
public string photo_link { get; set; }
public string thumb_link { get; set; }
public string type { get; set; }
public string base_url { get; set; }
}
public class Event_Context
{
public bool host { get; set; }
}
public class Venue
{
public int id { get; set; }
public string name { get; set; }
public float lat { get; set; }
public float lon { get; set; }
public bool repinned { get; set; }
public string address_1 { get; set; }
public string city { get; set; }
public string country { get; set; }
public string localized_country_name { get; set; }
}