-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathUntappd_Data.py
168 lines (135 loc) · 6.29 KB
/
Untappd_Data.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
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
import json
import requests
import urllib.request as url
import os.path
import os
import sys
from datetime import datetime,timedelta
if not os.path.exists('data/'):
os.mkdir('data/')
os.mkdir('data/badge_images/')
with open('user_information.txt','r') as f:
data = [x.strip() for x in f.readlines()]
username = data[0].split('= ')[1]
client_id = data[1].split('= ')[1]
client_secret = data[2].split('= ')[1]
access_token = data[3].split('= ')[1]
def get_first_data():
'''
download full data
'''
user = requests.get("https://api.untappd.com/v4/user/info/{}?client_id={}&client_secret={}".format(username,client_id,client_secret))
user_data = user.json()
total_checkins = user_data["response"]["user"]["stats"]["total_checkins"]
print("Total Checkins = {}".format(total_checkins))
checkins_data = requests.get("https://api.untappd.com/v4/user/checkins/?access_token={}".format(access_token)).json()
append_data = checkins_data["response"]["checkins"]["items"]
while (len(append_data) < total_checkins):
max_id = checkins_data["response"]["pagination"]["max_id"]
next_url = "https://api.untappd.com/v4/user/checkins/?max_id={}&access_token={}".format(max_id,access_token)
print("Downloading {} of {}".format(len(append_data),total_checkins))
checkins = requests.get(next_url)
checkins_data = checkins.json()
append_data.extend(checkins_data["response"]["checkins"]["items"])
with open("data/untappd_checkins.json", "w") as f:
json.dump(append_data, f)
return current_data
def get_updated_data():
'''
download full data
'''
with open('data/untappd_checkins.json') as json_file: # read in data from untappd api
current_data = json.load(json_file)
current_time = datetime.strptime(current_data[0]['created_at'],'%a, %d %b %Y %H:%M:%S %z').replace(tzinfo=None)
checkins = requests.get("https://api.untappd.com/v4/user/checkins/?access_token={}".format(access_token))
append_data = []
checkins_data = checkins.json()
for checkin in checkins_data["response"]["checkins"]["items"]:
new_time = datetime.strptime(checkin['created_at'],'%a, %d %b %Y %H:%M:%S %z').replace(tzinfo=None)
if new_time > current_time:
append_data.append(checkin)
count = 0
while new_time > current_time:
count += 1
if count % 10 == 0: print('{} checkins'.format(count))
max_id = checkins_data["response"]["pagination"]["max_id"]
next_url = "https://api.untappd.com/v4/user/checkins/?max_id={}&access_token={}".format(max_id,access_token)
checkins = requests.get(next_url)
checkins_data = checkins.json()
for checkin in checkins_data["response"]["checkins"]["items"]:
new_time = datetime.strptime(checkin['created_at'],'%a, %d %b %Y %H:%M:%S %z').replace(tzinfo=None)
if new_time > current_time:
append_data.append(checkin)
else:
break
append_data.extend(current_data)
with open("data/untappd_checkins.json", "w") as f:
json.dump(append_data, f)
return current_data
def get_unique_data_first():
'''
get unique data
'''
user = requests.get("https://api.untappd.com/v4/user/info/{}?client_id={}&client_secret={}".format(username,client_id,client_secret))
user_data = user.json()
total_checkins = user_data["response"]["user"]["stats"]["total_beers"]
print("Total Unique Checkins = {}".format(total_checkins))
unique_data = requests.get("https://api.untappd.com/v4/user/beers/{}?client_id={}&client_secret={}".format(username,client_id,client_secret)).json()
append_data = unique_data["response"]["beers"]["items"]
while (len(append_data) < total_checkins):
offset = unique_data["response"]['pagination']['offset']
next_url = 'https://api.untappd.com/v4/user/beers/{}?offset={}&client_id={}&client_secret={}'.format(username, offset,client_id,client_secret)
print("Downloading {} of {}".format(len(append_data),total_checkins))
unique_data = requests.get(next_url).json()
append_data.extend(unique_data["response"]["beers"]["items"])
with open("data/untappd_unique_beer.json", "w") as f:
json.dump(append_data, f)
def get_unique_data():
'''
get unique data
'''
with open('data/untappd_unique_beer.json') as json_file: # read in data from untappd api
current_data = json.load(json_file)
current_time = datetime.strptime(current_data[0]['first_created_at'],'%a, %d %b %Y %H:%M:%S %z').replace(tzinfo=None)
new_time = current_time + timedelta(1)
append_data = []
total = 0
while new_time > current_time:
next_url = 'https://api.untappd.com/v4/user/beers/' + username+'?offset=' + str(total) + '&client_id='+client_id+"&client_secret="+client_secret
checkins = requests.get(next_url)
checkins_data = checkins.json()
for checkin in checkins_data["response"]["beers"]["items"]:
new_time = datetime.strptime(checkin['first_created_at'],'%a, %d %b %Y %H:%M:%S %z').replace(tzinfo=None)
if new_time > current_time:
append_data.append(checkin)
else:
break
total += 25
append_data.extend(current_data)
with open("data/untappd_unique_beer.json", "w") as f:
json.dump(append_data, f)
def update_badges(current_data):
'''
updating badges
'''
with open('data/untappd_checkins.json') as json_file: # read in data from untappd api
data = json.load(json_file)[::-1]
for d in data:
if d['badges']['count'] > 0:
for badge in d['badges']['items']:
name = badge['badge_image']['lg']
if not os.path.isfile( "data/badge_images/"+name.split('/')[-1]):
try:
url.urlretrieve(name, "data/badge_images/"+name.split('/')[-1])
except:
pass
if __name__ == '__main__':
if os.path.exists('data/untappd_checkins.json'):
current_data = get_updated_data()
else:
current_data = get_first_data()
update_badges(current_data)
if os.path.exists('data/untappd_unique_beer.json'):
get_unique_data()
else:
get_unique_data_first()