Skip to content

Commit d92f226

Browse files
committed
Adding scripts to fetch untappd data
1 parent 54cdee3 commit d92f226

File tree

2 files changed

+153
-0
lines changed

2 files changed

+153
-0
lines changed

db.py

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import psycopg2
2+
3+
conn = psycopg2.connect("postgres:///beer_feed")
4+
5+
6+
with conn.cursor() as cur:
7+
cur.execute("""
8+
CREATE TABLE IF NOT EXISTS beers (
9+
auth_rating REAL,
10+
beer_abv REAL,
11+
beer_name TEXT,
12+
beer_style TEXT,
13+
beer_id INT PRIMARY KEY,
14+
rating_count INT,
15+
rating_score REAL,
16+
last_updated TIMESTAMP
17+
)
18+
""")
19+
20+
cur.execute("""
21+
CREATE TABLE IF NOT EXISTS breweries (
22+
brewery_id INT PRIMARY KEY,
23+
brewery_name TEXT,
24+
brewery_page_url TEXT,
25+
brewery_slug TEXT,
26+
brewery_type TEXT
27+
)
28+
""")
29+
30+
cur.execute("""
31+
CREATE TABLE IF NOT EXISTS venues(
32+
venue_id INT PRIMARY KEY,
33+
venue_name TEXT,
34+
venue_slug TEXT,
35+
lat REAL,
36+
lng REAL
37+
)
38+
"""
39+
)
40+
41+
42+
cur.execute("""
43+
CREATE TABLE IF NOT EXISTS checkins (
44+
beer_id INT references beers(beer_id),
45+
brewery_id INT references breweries(brewery_id),
46+
checkin_comment TEXT,
47+
checkin_id INT PRIMARY KEY,
48+
created_at TIMESTAMP,
49+
distance REAL,
50+
venue_id int references venues(venue_id)
51+
)
52+
""")
53+
54+
conn.commit()

fetch.py

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import untappd
2+
import json
3+
import os
4+
import psycopg2
5+
from datetime import datetime
6+
import time
7+
8+
9+
conn = psycopg2.connect("postgres:///beer_feed")
10+
11+
creds = json.load(open('.credentials.json'))
12+
13+
client = untappd.Untappd(
14+
client_id=creds["client_id"],
15+
client_secret=creds["client_secret"],
16+
redirect_url='localhost:8083',
17+
user_agent='letmein'
18+
)
19+
20+
21+
client.set_access_token(creds["user_token"])
22+
23+
24+
coords=40.714404, -73.942352
25+
26+
27+
def insert(tablename, data):
28+
keys = list(data.keys())
29+
values = ",".join([f"%({k})s" for k in keys])
30+
with conn.cursor() as cur:
31+
columns = ",".join(keys)
32+
cur.execute(f"""
33+
INSERT INTO {tablename} ({columns}) VALUES ({values})
34+
ON CONFLICT DO NOTHING
35+
""", data)
36+
conn.commit()
37+
38+
39+
40+
41+
def process_checkin(checkin):
42+
insert("venues", {
43+
"venue_id": checkin["venue"]["venue_id"],
44+
"venue_name": checkin["venue"]["venue_name"],
45+
"venue_slug": checkin["venue"]["venue_slug"],
46+
"lat": checkin["venue"]["location"]["lat"],
47+
"lng": checkin["venue"]["location"]["lng"],
48+
})
49+
50+
insert("breweries", {
51+
"brewery_id": checkin["brewery"]["brewery_id"],
52+
"brewery_name": checkin["brewery"]["brewery_name"],
53+
"brewery_page_url": checkin["brewery"]["brewery_page_url"],
54+
"brewery_slug": checkin["brewery"]["brewery_slug"],
55+
"brewery_type": checkin["brewery"]["brewery_type"],
56+
})
57+
58+
with conn.cursor() as cur:
59+
cur.execute("SELECT * FROM beers WHERE beer_id=%s", (checkin["beer"]["bid"],))
60+
res = cur.fetchall()
61+
62+
if len(res) == 0:
63+
try:
64+
time.sleep(2)
65+
beer = client.beer.info(checkin["beer"]["bid"])
66+
except Exception as e:
67+
print(e)
68+
insert("beers", {
69+
"beer_id": beer["response"]["beer"]["bid"],
70+
"auth_rating": beer["response"]["beer"]["auth_rating"],
71+
"beer_abv": beer["response"]["beer"]["beer_abv"],
72+
"beer_name": beer["response"]["beer"]["beer_name"],
73+
"beer_style": beer["response"]["beer"]["beer_style"],
74+
"rating_count": beer["response"]["beer"]["rating_count"],
75+
"rating_score": beer["response"]["beer"]["rating_score"],
76+
"last_updated": datetime.now()
77+
})
78+
79+
insert("checkins", {
80+
"beer_id": checkin["beer"]["bid"],
81+
"brewery_id": checkin["brewery"]["brewery_id"],
82+
"checkin_comment": checkin["checkin_comment"],
83+
"checkin_id": checkin["checkin_id"],
84+
"created_at": checkin["created_at"],
85+
"venue_id": checkin["venue"]["venue_id"],
86+
})
87+
88+
89+
with conn.cursor() as cur:
90+
cur.execute("SELECT MAX(checkin_id) FROM checkins")
91+
max_id, = cur.fetchone()
92+
93+
checkins = client.thepub.local(lat=coords[0], lng=coords[1], radius=10, dist_pref="km", min_id=max_id)
94+
print(f'Fetched {len(checkins["response"]["checkins"]["items"])} new checkins!')
95+
96+
# iterate in reverse order to process the oldest first. That way if we crash, we won't create gaps in the feed
97+
for checkin in checkins["response"]["checkins"]["items"][::-1]:
98+
process_checkin(checkin)
99+

0 commit comments

Comments
 (0)