-
Notifications
You must be signed in to change notification settings - Fork 25
/
untappd.py
402 lines (362 loc) · 16.1 KB
/
untappd.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#!/usr/bin/env python
"""
Author: Micah Hoffman (@WebBreacher)
Purpose: To look up a user on Untappd.com and provide drinking profile
"""
import argparse
import geocoder
import gmplot
import googlemaps
import re
import requests
import time
from bs4 import BeautifulSoup
from requests.packages.urllib3.exceptions import InsecureRequestWarning
from geocode_api_keys import *
####
# Functions
####
# Parse command line input
parser = argparse.ArgumentParser(description='Grab Untappd user activity')
parser.add_argument('-r', '--recent', action='store_true', help='Just dump the locations of the last beers they logged')
parser.add_argument('-u', '--user', required=True, help='Username to research')
args = parser.parse_args()
def get_mean(lst):
return float(sum(lst) / len(lst))
def get_data_from_untappd(url):
# Setting up and Making the Web Call
try:
user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:74.0) Gecko/20100101 Firefox/74.0'
headers = {'User-Agent': user_agent}
# Make web request for that URL and don't verify SSL/TLS certs
response = requests.get(url, headers=headers, verify=False)
return response.text
except Exception as e:
print('[!] ERROR - Untappd issue: {}'.format(str(e)))
exit(1)
def get_user_data(passed_user):
# Parsing user information
url = 'https://untappd.com/user/{}'.format(passed_user)
print("\n[ ] USER DATA: Requesting {}".format(url))
resp = get_data_from_untappd(url)
html_doc = BeautifulSoup(resp, 'html.parser')
user1 = html_doc.find_all('span', 'stat')
if user1:
return user1
def get_friend_data(passed_user):
friends1 = []
# Parsing user friend information
url = 'https://untappd.com/user/{}/friends'.format(passed_user)
print("\n[ ] FRIEND DATA: Requesting 25 friends from {}".format(url))
resp = get_data_from_untappd(url)
html_doc = BeautifulSoup(resp, 'html.parser')
user1 = html_doc.find_all('div', 'user')
for u in user1:
friends1.append(u.text.encode('ascii', 'ignore').strip())
if friends1:
return friends1
def get_beers_data(passed_user):
beers_drank = []
# Parsing user beer information
url = 'https://untappd.com/user/{}/beers'.format(passed_user)
print("\n[ ] BEER CONSUMPTION DATA: Requesting {}".format(url))
resp = get_data_from_untappd(url)
html_doc = BeautifulSoup(resp, 'html.parser')
beers = html_doc.find_all('abbr', 'date-time')
for b in beers:
beers_drank.append(b.text.strip())
if beers_drank:
return beers_drank
def get_beersonly_data(passed_user):
beers_drank = []
# Parsing user beer information
url = 'https://untappd.com/user/{}/'.format(passed_user)
print("\n[ ] BEER LOCATION DATA: Requesting {}\n".format(url))
resp = get_data_from_untappd(url)
html_doc = BeautifulSoup(resp, 'html.parser')
beers = html_doc.find_all('div', 'checkin')
for b in beers:
beers_drank.append(b.text.strip())
if beers_drank:
return beers_drank
def get_venue_data(passed_user):
# Parsing check-in location information
drinkslatslongs = []
drinkslatslongstitle = []
if google_api_key != 'GOOGLE_API_KEY':
gmaps = googlemaps.Client(key=google_api_key)
url = 'https://untappd.com/user/{}/venues?type=&sort=highest_checkin'.format(passed_user)
print("\n[ ] VENUE DATA: Requesting {}\n".format(url))
resp = get_data_from_untappd(url)
matchvenueobj = re.findall(
'data-href=":view/name" href="/venue/([0-9]+)">(.+?)</a>.*?class="address">(.+?)</div>.*?Check-ins:</strong>.*?([0-9]+).*?</p>',
resp, re.DOTALL)
if matchvenueobj:
print(' {:>5} {}, {}'.format('Checkins', 'Name', 'Address (Geocode)'))
print(' -------- ----------------------------------------------------')
for venue1 in matchvenueobj:
place = '{}, {}'.format(venue1[1],
venue1[2].replace('\t', '').replace('\n', ''))
if google_api_key != 'GOOGLE_API_KEY':
try:
g = gmaps.geocode(place)
loc = g[0]['geometry']['location']['lat'], g[0]['geometry']['location']['lng']
except:
pass
else:
loc = ''
print(' {:>5} {}, {} {}'.format(
venue1[3], venue1[1],
venue1[2].replace('\t', '').replace('\n', ''), loc))
if google_api_key != 'GOOGLE_API_KEY':
if g:
# Add the correct number of visits to the lat/lon list for weighting
for num in range(int(venue1[3])):
drinkslatslongs.append(tuple(loc))
drinkslatslongstitle.append([loc, venue1[3], venue1[1]])
else:
print('[-] No Venue data found')
if google_api_key != 'GOOGLE_API_KEY':
drink_lats, drink_longs = zip(*drinkslatslongs)
# Compute the center Lat and Long to center the map
center_lat = get_mean(drink_lats)
center_long = get_mean(drink_longs)
gmap = gmplot.GoogleMapPlotter(center_lat, center_long, 6)
# Create the points/heatmap/circles on the map
gmap.coloricon = "http://www.googlemapsmarkers.com/v1/%s/"
for latlng, num, title in drinkslatslongstitle:
try:
gmap.marker(latlng[0], latlng[1], title='{} beers logged at {}'.format(num, title))
except:
pass
gmap.heatmap(drink_lats, drink_longs, 1, 100)
gmap.scatter(drink_lats, drink_longs, '#333333', size=1, marker=False)
gmap.plot(drink_lats, drink_longs, '#FF33FF', edge_width=1)
outfile = 'untappd_map_{}_{}.html'.format(args.user, str(int(time.time())))
gmap.draw(outfile)
print("\n[ ] HTML output file named {} was written to disk.\n".format(outfile))
###########################
# Start
###########################
# Suppress HTTPS warnings
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
###############
# Get User info
###############
user = get_user_data(args.user)
if user:
print('\n Total Beers: {:>6}'.format(user[0].text))
print(' Total Unique: {:>6}'.format(user[1].text))
print(' Total Badges: {:>6}'.format(user[2].text))
print(' Total Friends: {:>6}'.format(user[3].text))
if args.recent:
# Just grab the beers they logged and print them
beers = get_beersonly_data(args.user)
for beer in beers:
locations = re.findall(r'is drinking.*?at (.+?)\n.*([0-9][0-9] [A-Z][a-z][a-z] [0-9][0-9][0-9][0-9] [0-9][0-9]\:[0-9][0-9]\:[0-9][0-9])', beer, re.MULTILINE|re.DOTALL)
for content in locations:
print(' {} - {}'.format(content[1], content[0]))
exit()
###############
# Get friends of target
###############
friends = get_friend_data(args.user)
if friends:
print('\n {: ^20} {: ^18} {: ^20}'.format('Account','Name','Location'))
print(' -------------------- ------------------ --------------------')
for friend in friends:
row = friend.decode('utf-8').split("\n")
if len(row) == 3:
print(' {1: <20} {0: <18} {2: <20}'.format(*row))
else:
print(' {1: <20} {0: <18}'.format(*row))
else:
print('[-] No friends found')
###############
# Get Beer Drinking dates/times
###############
when = list(set(get_beers_data(args.user))) # We need to remove duplicates from the list
days_of_week = []
days_of_month = []
hours_of_day = []
if when:
for beer_date_time in when:
dates = beer_date_time.split()
days_of_week.append(dates[0].strip(','))
days_of_month.append(dates[1])
hours_min_sec = dates[4].split(':')
hours_of_day.append(hours_min_sec[0])
# Days of Week Analysis
sun = days_of_week.count('Sun')
mon = days_of_week.count('Mon')
tue = days_of_week.count('Tue')
wed = days_of_week.count('Wed')
thu = days_of_week.count('Thu')
fri = days_of_week.count('Fri')
sat = days_of_week.count('Sat')
print('[*] Drinking Patterns (Last 25 beers) - Days of Week')
print(' Day ( #) : HISTOGRAM')
print(' ---------------------------------')
print(' Mon ({:>2}) : {}'.format(mon, mon * 'x'))
print(' Tue ({:>2}) : {}'.format(tue, tue * 'x'))
print(' Wed ({:>2}) : {}'.format(wed, wed * 'x'))
print(' Thu ({:>2}) : {}'.format(thu, thu * 'x'))
print(' Fri ({:>2}) : {}'.format(fri, fri * 'x'))
print(' Sat ({:>2}) : {}'.format(sat, sat * 'x'))
print(' Sun ({:>2}) : {}'.format(sun, sun * 'x'))
print('')
# Hours of Day Analysis
h6 = hours_of_day.count('06')
h7 = hours_of_day.count('07')
h8 = hours_of_day.count('08')
h9 = hours_of_day.count('09')
h10 = hours_of_day.count('10')
h11 = hours_of_day.count('11')
h12 = hours_of_day.count('12')
h13 = hours_of_day.count('13')
h14 = hours_of_day.count('14')
h15 = hours_of_day.count('15')
h16 = hours_of_day.count('16')
h17 = hours_of_day.count('17')
h18 = hours_of_day.count('18')
h19 = hours_of_day.count('19')
h20 = hours_of_day.count('20')
h21 = hours_of_day.count('21')
h22 = hours_of_day.count('22')
h23 = hours_of_day.count('23')
h0 = hours_of_day.count('00')
h1 = hours_of_day.count('01')
h2 = hours_of_day.count('02')
h3 = hours_of_day.count('03')
h4 = hours_of_day.count('04')
h5 = hours_of_day.count('05')
print('[*] Drinking Patterns (Last 25 beers) - Hours of Day')
print(' Hour ( #) : HISTOGRAM')
print(' ---------------------------------')
print(' 06:00 ({:>2}) : {}'.format(h6, h6 * 'x'))
print(' 07:00 ({:>2}) : {}'.format(h7, h7 * 'x'))
print(' 08:00 ({:>2}) : {}'.format(h8, h8 * 'x'))
print(' 09:00 ({:>2}) : {}'.format(h9, h9 * 'x'))
print(' 10:00 ({:>2}) : {}'.format(h10, h10 * 'x'))
print(' 11:00 ({:>2}) : {}'.format(h11, h11 * 'x'))
print(' 12:00 ({:>2}) : {}'.format(h12, h12 * 'x'))
print(' 13:00 ({:>2}) : {}'.format(h13, h13 * 'x'))
print(' 14:00 ({:>2}) : {}'.format(h14, h14 * 'x'))
print(' 15:00 ({:>2}) : {}'.format(h15, h15 * 'x'))
print(' 16:00 ({:>2}) : {}'.format(h16, h16 * 'x'))
print(' 17:00 ({:>2}) : {}'.format(h17, h17 * 'x'))
print(' 18:00 ({:>2}) : {}'.format(h18, h18 * 'x'))
print(' 19:00 ({:>2}) : {}'.format(h19, h19 * 'x'))
print(' 20:00 ({:>2}) : {}'.format(h20, h20 * 'x'))
print(' 21:00 ({:>2}) : {}'.format(h21, h21 * 'x'))
print(' 22:00 ({:>2}) : {}'.format(h22, h22 * 'x'))
print(' 23:00 ({:>2}) : {}'.format(h23, h23 * 'x'))
print(' 00:00 ({:>2}) : {}'.format(h0, h0 * 'x'))
print(' 01:00 ({:>2}) : {}'.format(h1, h1 * 'x'))
print(' 02:00 ({:>2}) : {}'.format(h2, h2 * 'x'))
print(' 03:00 ({:>2}) : {}'.format(h3, h3 * 'x'))
print(' 04:00 ({:>2}) : {}'.format(h4, h4 * 'x'))
print(' 05:00 ({:>2}) : {}'.format(h5, h5 * 'x'))
print('')
# Day of Month Analysis
d0 = days_of_month.count('00')
d1 = days_of_month.count('01')
d2 = days_of_month.count('02')
d3 = days_of_month.count('03')
d4 = days_of_month.count('04')
d5 = days_of_month.count('05')
d6 = days_of_month.count('06')
d7 = days_of_month.count('07')
d8 = days_of_month.count('08')
d9 = days_of_month.count('09')
d10 = days_of_month.count('10')
d11 = days_of_month.count('11')
d12 = days_of_month.count('12')
d13 = days_of_month.count('13')
d14 = days_of_month.count('14')
d15 = days_of_month.count('15')
d16 = days_of_month.count('16')
d17 = days_of_month.count('17')
d18 = days_of_month.count('18')
d19 = days_of_month.count('19')
d20 = days_of_month.count('20')
d21 = days_of_month.count('21')
d22 = days_of_month.count('22')
d23 = days_of_month.count('23')
d24 = days_of_month.count('24')
d25 = days_of_month.count('25')
d26 = days_of_month.count('26')
d27 = days_of_month.count('27')
d28 = days_of_month.count('28')
d29 = days_of_month.count('29')
d30 = days_of_month.count('30')
d31 = days_of_month.count('31')
print('[*] Drinking Patterns (Last 25 beers) - Day of Month')
print(' Day (#) : HISTOGRAM')
print(' ---------------------------------')
print(' 00 ({:>2}) : {}'.format(d0, d0 * 'x'))
print(' 01 ({:>2}) : {}'.format(d1, d1 * 'x'))
print(' 02 ({:>2}) : {}'.format(d2, d2 * 'x'))
print(' 03 ({:>2}) : {}'.format(d3, d3 * 'x'))
print(' 04 ({:>2}) : {}'.format(d4, d4 * 'x'))
print(' 05 ({:>2}) : {}'.format(d5, d5 * 'x'))
print(' 06 ({:>2}) : {}'.format(d6, d6 * 'x'))
print(' 07 ({:>2}) : {}'.format(d7, d7 * 'x'))
print(' 08 ({:>2}) : {}'.format(d8, d8 * 'x'))
print(' 09 ({:>2}) : {}'.format(d9, d9 * 'x'))
print(' 10 ({:>2}) : {}'.format(d10, d10 * 'x'))
print(' 11 ({:>2}) : {}'.format(d11, d11 * 'x'))
print(' 12 ({:>2}) : {}'.format(d12, d12 * 'x'))
print(' 13 ({:>2}) : {}'.format(d13, d13 * 'x'))
print(' 14 ({:>2}) : {}'.format(d14, d14 * 'x'))
print(' 15 ({:>2}) : {}'.format(d15, d15 * 'x'))
print(' 16 ({:>2}) : {}'.format(d16, d16 * 'x'))
print(' 17 ({:>2}) : {}'.format(d17, d17 * 'x'))
print(' 18 ({:>2}) : {}'.format(d18, d18 * 'x'))
print(' 19 ({:>2}) : {}'.format(d19, d19 * 'x'))
print(' 20 ({:>2}) : {}'.format(d20, d20 * 'x'))
print(' 21 ({:>2}) : {}'.format(d21, d21 * 'x'))
print(' 22 ({:>2}) : {}'.format(d22, d22 * 'x'))
print(' 23 ({:>2}) : {}'.format(d23, d23 * 'x'))
print(' 24 ({:>2}) : {}'.format(d24, d24 * 'x'))
print(' 25 ({:>2}) : {}'.format(d25, d25 * 'x'))
print(' 26 ({:>2}) : {}'.format(d26, d26 * 'x'))
print(' 27 ({:>2}) : {}'.format(d27, d27 * 'x'))
print(' 28 ({:>2}) : {}'.format(d28, d28 * 'x'))
print(' 29 ({:>2}) : {}'.format(d29, d29 * 'x'))
print(' 30 ({:>2}) : {}'.format(d30, d30 * 'x'))
print(' 31 ({:>2}) : {}'.format(d31, d31 * 'x'))
print('')
# Drinking Levels from https://www.niaaa.nih.gov/alcohol-health/overview-alcohol-consumption/moderate-binge-drinking
# Binge Drinking = 5+ drinks same day
# Heavy Drinking = 5+ drinks on 5+ days per month
# Since we only get 25 total beers now, I'm changing this to 4 days per month ~=heavy drinker
binge_drink_counter = 0
for day in ["%02d" % i for i in range(32)]:
if days_of_month.count(day) >= 5:
print('[!] *ALERT - Due to drinking {} beers on day {}, user may be a "Binge Drinker"'.format(
days_of_month.count(day), day))
print('[!] Examine times they drank the beers below. If 5+ drinks in < 2 hours, then binge.')
# Cycle back through the data to pull out H:M:S for beers drank on a certain day
times_posted = []
for beer_date_time in when:
dates = beer_date_time.split()
if day == dates[1]:
times_posted.append(dates[4])
#print('[!] {}'.format(dates[4]))
for tm in sorted(times_posted):
print('[!] {}'.format(tm))
binge_drink_counter += 1
if binge_drink_counter >= 4:
print(
'[!] *ALERT = Due to {} days of Binge Drinking, user may be a "Heavy Drinker"'.format(binge_drink_counter))
elif binge_drink_counter >= 1:
print('[!] * This script does not examine the amount of time between drinks, which is important.')
print(
'[!] * https://www.niaaa.nih.gov/alcohol-health/overview-alcohol-consumption/moderate-binge-drinking')
else:
print('[-] No recent check-in dates/times found')
###############
# Get Venue info
###############
get_venue_data(args.user)