def do_search(args): bgg = BGG() res = bgg.search(args.search) tot = int(res['items']['total']) ids = [] if tot > 1: ids = [o['id'] for o in res['items']['item']] elif tot == 1: ids = [res['items']['item']['id']] res = bgg.boardgame(ids, stats=False) #import json #print(json.dumps(res, indent=4)) res = res['items']['item'] if not isinstance(res, (list, tuple)): res = [res] for item in res: name = '' if isinstance(item['name'], list): name = item['name'][0]['value'] else: name = item['name']['value'] print(f'\033[0;33m{name}\033[0m: {item["thumbnail"]["TEXT"]}')
def get_data(game_id): """get data for a single game""" print(game_id) bgg = BGG() game_tree = bgg.boardgame(game_id, comments=False, stats=True) if (len(game_tree['items']) == 1): game_tree = bgg.boardgameexpansion(game_id, comments=False, stats=True) game = game_tree['items']['item'] sleep(2) return game
def get_ratings(username): """gets ratings for a specified user""" bgg = BGG() collection = bgg.get_collection(username, rated=1, stats=1) coll_list = collection['items']['item'] mycoll = [] for game in coll_list: game_id = int(game['objectid']) rating = float(game['stats']['rating']['value']) mycoll.append((game_id, rating)) coll_df = pd.DataFrame(mycoll, columns=['id', 'rating']).set_index('id') return coll_df
def get_info_dicts(self): call_id_lst = [] i_one = 0 i_two = self.n_games if self.n_games < 100 else 100 for r in range(math.ceil(self.n_games/100)): call_id_lst.append([x[0] for x in self.id_game_lst[i_one:i_two]]) i_one += 100 i_two += 100 random_sec = np.random.uniform(5,6,[1000,]) info_dicts=[] for current_call_lst in call_id_lst: if len(call_id_lst) > 1: time.sleep(np.random.choice(random_sec)) info_dicts.append(BGG().boardgame(current_call_lst, stats=True)) return info_dicts
def get_comments(game_id): """gets coments for a single game""" bgg = BGG() game_tree = bgg.boardgame(game_id, comments=True, pagesize=10) if (len(game_tree['items']) == 1): game_tree = bgg.boardgameexpansion(game_id, comments=True, pagesize=10) game = game_tree['items']['item'] ncomments = int(game['comments']['totalitems']) try: title = game['name']['value'] except: title = game['name'][0]['value'] comments = [] print(title) for i in range(int(np.ceil(ncomments / 100))): try: game_tree = bgg.boardgame(game_id, comments=True, pagesize=100, page=i + 1) if (len(game_tree['items']) == 1): game_tree = bgg.boardgameexpansion(game_id, comments=True, pagesize=100, page=i + 1) except: continue game = game_tree['items']['item'] comments.extend(game['comments']['comment']) sleep(1.5) print(i) comments = {'id': game_id, 'title': title, 'comments': comments} return comments
from datetime import datetime, timedelta from flask import Flask, render_template, jsonify from libbgg.apiv2 import BGG from report import sell_report app = Flask(__name__) app.bgg = BGG() @app.route('/') def main(): return render_template('index.html') @app.route('/sellreport/u/<username>') def generate_sell_report(username): oneyearago = (datetime.utcnow() - timedelta(days=365)).strftime('%Y-%m-%d') try: report_text = sell_report(username.lower(), since=oneyearago) except Exception: return jsonify( dict(report="Error: Invalid username or unable to fetch user data." )), 400 return jsonify(dict(report=report_text)) if __name__ == '__main__': app.run()
with open('data/official_game_dict_p2.pkl', 'rb') as fp: id_game_dict = pickle.load(fp) with open('data/game_ids_170516.pkl', 'rb') as fp: id_game_lst = pickle.load(fp) #make id,game key,value pair dictionary #was using a list here from get_ids.py, but now using updated dictionary #from get_official_names.py #id_game_dict = {x[0] : x[1] for x in id_game_lst[:-1]} client = MongoClient() #set database that collection is in database = client.bgg #collection for stats variables to go in stats_coll = database.game_stats #making call to api for dictionary object conn = BGG() ''' the index in the id_game_lst specified here will determine how many games are looped through note an error is thrown if only one game is in the list one_get_info.py is for this purpose ''' i_one = 0 i_two = 100 call_id_lst = [] for r in range(145): call_id_lst.append([x[0] for x in id_game_lst[i_one:i_two]]) i_one += 100 i_two += 100 stat_results = get_stats_results()
def get_ratings_comments_results(game_id_list_path, error_path, start_game=0, start_page=1, num_games=1000, database="bgg_test", collection="game_comments_test"): error_lst = [] conn = BGG() #open pickle file with ids,games,rating for use with open(game_id_list_path, 'rb') as fp: id_game_lst = pickle.load(fp) client = MongoClient() #database for comments to go into database = "client.{}".format(database) #collection for stats variables to go in comments_coll = "database.{}".format(collection) print(comments_coll) id_game_dict = {x[0]: x[1] for x in id_game_lst[:-1]} #reverse lookup for dictionary # next(key for key, value in id_game_dict.items() if value == 'tzolk-mayan-calendar') #this range identifies the games that will be databased from the #id_game_lst call_id_lst = [x[start_game] for x in id_game_lst[:num_games]] print("Number of games:", len(call_id_lst)) for game_id in call_id_lst: random_sec = np.random.uniform(5, 6, [ 10000, ]) current_id = game_id current_game = id_game_dict.get(current_id) print("current_id:", current_id) print("current_game:", current_game) #specify starting page number, should be 1 unless testing page = start_page while page != None: time.sleep(np.random.choice(random_sec)) print("page:", page) try: comment_results = conn.boardgame(game_id, comments=True, page=page, pagesize=100) #print(comment_results) try: comments = comment_results['items']['item']['comments'][ 'comment'] # print("comments:" ,comments) print("length:", len(comments)) for entry in comments: print(len(comments)) print(entry) try: rating = float(entry.get('rating')) except ValueError: rating = None comments_coll.insert_one({ "game": current_game, "game_id": str(current_id), "username": entry.get('username'), "rating": rating, "comment": entry.get('value') }) page += 1 #time.sleep(np.random.choice(random_sec)) except KeyError: #print("no comments") page = None except KeyboardInterrupt: raise except: error_lst.append(( current_game, current_id, page, )) #print(error_lst) with open(error_path, 'wb') as fp: pickle.dump(error_lst, fp) page += 1 return error_lst
# Register your models here. from django.http import HttpResponseRedirect from django.forms import Textarea from django.contrib import admin, messages from django.db import models from inline_actions.admin import InlineActionsMixin from inline_actions.admin import InlineActionsModelAdminMixin from libbgg.apiv2 import BGG from .models import Player, Game bgg = BGG() def getformfield(request, pk, field): formfield = "" items = request.POST.dict().items() for key, value in items: print(key) if key.startswith("game_set-") and key.endswith("-id") and value == pk: deskey = key.replace("-id", "-" + field) formfield = request.POST[deskey] return formfield from django.utils.safestring import mark_safe def getbgginfo(name, id, url): newid = id newurl = url
import json from libbgg.apiv2 import BGG conn = BGG() # will capture all description text for a game # stat_results = conn.boardgame(169786 , stats=True) # description = stat_results['items']['item']['description']['TEXT'] #was thinking this was necessary, but found the above is accessible via #standard dictionary style access # scythe = json.dumps(results) # scythe_load = json.loads(scythe) #this will get every single rating and comment if they made one #if the page specified does not exist a value error is raised, thus probably a #good way to end a loop comment_results = conn.boardgame(169786, ratingcomments=True, page=100, pagesize=100) print(comment_results['items']['item']['comments']['comment'])