|
| 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