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_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
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
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'])