Created
September 21, 2017 18:53
-
-
Save Rsilnav/a80e875fac0b5b474b9fe3bf65dc77ce to your computer and use it in GitHub Desktop.
update.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import sqlite3 | |
| import urllib.request, json | |
| locale = "es_ES" # Most used would be en_GB or en_US | |
| region = "eu" # Or "us" | |
| realm = "cthun" # Without symbols nor spaces | |
| api_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # Your API key as explained in the post | |
| url_address = "https://" + region + ".api.battle.net/wow/auction/data/" + realm + "?locale=" + locale + "&apikey=" + api_key | |
| url = urllib.request.urlopen(url_address) | |
| data = json.loads(url.read().decode()) | |
| last = data['files'][0]['lastModified'] | |
| db = sqlite3.connect('wow.db') | |
| c = db.cursor() | |
| c.execute('SELECT * FROM status WHERE realm=?', [realm]) | |
| row = c.fetchone() | |
| updated = True | |
| if row == None: | |
| updated = False | |
| for file in data['files']: | |
| if updated and file['lastModified'] != row[1]: | |
| updated = False | |
| if updated: | |
| print("Database already updated!") | |
| else: | |
| c.execute('DELETE FROM auction') | |
| c.execute('DELETE FROM status WHERE realm=?', [realm]) | |
| for file in data['files']: | |
| url = urllib.request.urlopen(file['url']) | |
| data = json.loads(url.read().decode()) | |
| auctions = data['auctions'] | |
| for auc in auctions: | |
| c.execute('INSERT INTO auction (auc, item, owner, buyout, qty) VALUES (?,?,?,?,?)', [auc['auc'], auc['item'], auc['owner'], auc['buyout'], auc['quantity']]) | |
| c.execute('DELETE FROM auction WHERE buyout=0') | |
| c.execute('INSERT INTO status VALUES (?, ?)', [realm, last]) | |
| db.commit() | |
| print("Database wasn't updated!") | |
| print("I have added %s auctions =)" % (len(auctions))) | |
| db.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment