Skip to content

Instantly share code, notes, and snippets.

@selwyth
Last active February 19, 2024 19:55
Show Gist options
  • Save selwyth/da7775647dca941c1175 to your computer and use it in GitHub Desktop.
Save selwyth/da7775647dca941c1175 to your computer and use it in GitHub Desktop.
Using BGG XML API2
user = raw_input('Enter BGG username')
url = 'http://www.boardgamegeek.com/xmlapi2/collection?username='
import requests
import time
# return collection for any user, but wait 2 seconds and retry if error. 10 total attempts.
def coll_final(USER):
for i in range(10): # try 10 times
r = requests.get(url + USER + "&rated=1&excludesubtype=boardgameexpansion&stats=1")
if r.status_code == 202:
time.sleep(2)
i += 1
continue
else:
print('# of query attempts is {}'.format(i+1))
data = r.content
return data
break
coll = coll_final(user)
import requests
import time
# returns a `meta` list that contains XML objects for the first 20 games in the db (untested)
# if fail, wait 5 seconds before retrying the same game (10 attempts)
ids = range(1,21)
meta = []
for j in ids:
gameurl = 'http://www.boardgamegeek.com/xmlapi2/thing?type=boardgame&stats=1&id=' + j
r = requests.get(gameurl)
for i in range(10):
r = requests.get(gameurl)
if r.status_code == 200:
meta.append(r.content)
break
else:
time.sleep(5)
i += 1
continue
from lxml import etree
parsed = etree.fromstring(coll)
for i in parsed:
print i.items()
ids = [x.items()[1][1] for x in parsed]
[rating.attrib for rating in parsed.iter('rating')]
# try parsed.get('rating')?
# Should be obsolete, keeping around for reference
from urllib2 import Request, urlopen, URLError
coll_req = Request('http://www.boardgamegeek.com/xmlapi2/collection?username=selwyth')
item_req = Request('http://www.boardgamegeek.com/xmlapi2/thing?id=53223')
item_req2 = Request('http://www.boardgamegeek.com/xmlapi2/thing?id=53223,53243')
print 'type of coll_req is %s' % type(coll_req)
response = urlopen(coll_req)
print 'type of response is %s' % type(response)
coll_output = response.read()
print 'type of coll_output is %s' % type(coll_output)
# trying this out
# import requests
# query_string = 'http://www.boardgamegeek.com/xmlapi2/thing?id={}'.format()
# bggapi = requests.get(query_string)
# print(bggapi.text)
def coll(user):
# return collection for any user
xml_string = 'http://www.boardgamegeek.com/xmlapi2/collection?username={}'.format(user)
coll_req = Request(xml_string)
response = urlopen(coll_req)
return response.read()
print coll('selwyth')
import time
def coll_final(user):
# return collection for any user (building off base earlier), but wait 1 second and retry if error
for i in range(10): # try 10 times
coll(user)
if coll(user).find("Please try again later for access.") > -1:
time.sleep(1)
i += 1
continue
else:
print '# of query attempts is {}'.format(i)
return coll(user)
break
test = coll_final('blindspot')
from lxml import etree
parsed = etree.fromstring(test)
for i in parsed:
print i.items()
parsed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment