-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpyglialo.py
63 lines (52 loc) · 1.99 KB
/
pyglialo.py
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
# -*- coding: utf-8 -*-
import datetime
import json
import random
import time
import urllib.error
import urllib.parse
import urllib.request
from config import URL_EVENTS, URL_RSVPS
def get_data_from_url(url):
response = urllib.request.urlopen(url)
data = json.loads(response.read().decode("utf-8"))
return data
class PyGlialo(object):
event_id = None
event_rsvps = None
event = None
list_of_winners = None
winner = None
def __init__(self):
self.list_of_winners = []
def load_meetup_data(self):
data = get_data_from_url(URL_EVENTS)
if data:
self.event = min(data, key=lambda event: abs(event['time'] - time.time()))
else:
self.event = {}
self.event_id = self.event.get('id', None)
self.event_rsvps = get_data_from_url(URL_RSVPS.format(id=self.event_id))
self.list_of_winners = []
def extract_safe_winner(self):
self.winner = None
if self.event_rsvps and len(self.event_rsvps):
lucky_number = random.randint(0, abs(len(self.event_rsvps) - 1))
winner = self.event_rsvps[lucky_number]
self.event_rsvps.remove(winner)
if winner['response'] == 'yes':
self.winner = {
'name': winner['member']['name'],
'member_id': winner['member']['id'],
'photo_url': winner['member'].get('photo', {}).get('photo_link', '/static/img/no_image.png')
}
else:
self.extract_safe_winner()
def save_winners_list(self):
file_date = datetime.datetime.now()
with open("winner_list_{}.txt".format(file_date.strftime("%Y-%m-%d")), "w") as out_file:
out_file.write("File generato il: {}\n".format(file_date.strftime("%Y-%m-%d_%H:%M:%S")))
out_file.write("Lista vincitori per i goodies\n")
for winner in self.list_of_winners:
out_file.write("{}\n".format(winner))
pyglialo = PyGlialo()