def count_to_play_count(username, pagesize): filename = "totalplays.xml" path = os.path.join(os.getcwd(), filename) url = f'http://www.boardgamegeek.com/xmlapi2/plays?username={username}&pagesize=10' if os.path.isfile(path) is False or new_download() is True: download_xml = DownloadXML(url, filename) download_xml.download() read_xml = ReadXML() read_xml.read_xml_file(path) return math.ceil(read_xml.play_count / float(pagesize)), read_xml.play_count
def update(self, num_plays, pagesize=10): from DatabaseInteractions import add_records val = False url = self.url.replace('pagesize=100', f'pagesize={pagesize}') download = DownloadXML(url, 'update.xml') download.download() read = ReadXML() read.read_xml_file('update.xml') if read.play_count > num_plays: how_many = read.play_count - num_plays if how_many > pagesize: val = self.update(num_plays, how_many) add_records(read.plays) return val
def read_xml(self): """ Reads all the xml files that were download by retrieve_xml :return: None """ read_xml = ReadXML() read_xml.read_xml_all(os.path.join(os.getcwd(), "plays"), self.count_to) for play in read_xml.plays: if self.player: for player in play.players: if player.__getattribute__( self.change_attrib) == self.change_from: self.play_nums.append(play) break else: if str(play.__getattribute__( self.change_attrib)) == self.change_from: self.play_nums.append(play) print(self.play_nums)
def __init__(self): self.pagesize = 100 self.username = "******" self.ignore = [ 'Keith', 'Paul', 'Dempsey', 'Other', 'Kelly', 'Alyssa', 'Player 6', 'Mark', 'Player 7', 'Beulah', 'Besa', 'Player 5', 'Raymon', 'Play 2', 'Jay', 'Play 6', 'Play 5', 'Play 4', 'Play 3', 'Anthony', 'Bill' ] self.url = f'http://www.boardgamegeek.com/xmlapi2/plays?username={self.username}' \ f'&pagesize={str(self.pagesize)}&page=' self.filename = "plays.xml" self.downloadXML = DownloadXML(self.url, self.filename) self.readXML = ReadXML() self.re_download = False self.count_to = BGGModule.Functions.count_to(self.username, self.pagesize) self.left_off = 1
class ReadXMLTests(unittest.TestCase): readxml = ReadXML() attr1 = ['id', 'date', 'quantity', 'length', 'incomplete', 'nowinstats', 'location', 'game_name', 'gameid'] attr2 = ['username', 'userid', 'name', 'startposition', 'colour', 'score', 'new', 'rating', 'win'] xml_data = [PlaysXMLDataset(id=38023491, date="2019-09-29", quantity=1, length=0, incomplete=0, nowinstats=0, location="", game_name="Asking for Trobils", gameid=156442), PlaysXMLDataset(id=38021178, date="2019-09-29", quantity=1, length=0, incomplete=0, nowinstats=0, location="", game_name="Coup", gameid=131357)] xml_data[0].players = [ PlayerXMLDataset(username="******", userid=508171, name="Richard Allen", startposition=1, colour="", score=32, new=False, rating=0, win=False), PlayerXMLDataset(username="", userid=0, name="Jeff", startposition=2, colour="", score=30, new=False, rating=0, win=False), PlayerXMLDataset(username="", userid=0, name="Tanya", startposition=3, colour="", score=41.1, new=False, rating=0, win=True), PlayerXMLDataset(username="", userid=0, name="Arden", startposition=4, colour="", score=35, new=False, rating=0, win=False), PlayerXMLDataset(username="", userid=0, name="Shelly", startposition=5, colour="", score=29, new=False, rating=0, win=False), PlayerXMLDataset(username="", userid=0, name="Alex", startposition=6, colour="", score=32, new=False, rating=0, win=False)] xml_data[1].players = [ PlayerXMLDataset(username="******", userid=508171, name="Richard Allen", startposition=1, colour="", score=0, new=False, rating=0, win=False), PlayerXMLDataset(username="", userid=0, name="Jeff", startposition=2, colour="", score=0, new=False, rating=0, win=False), PlayerXMLDataset(username="", userid=0, name="Tanya", startposition=3, colour="", score=0, new=False, rating=0, win=False), PlayerXMLDataset(username="", userid=0, name="Damon", startposition=4, colour="", score=0, new=False, rating=0, win=False), PlayerXMLDataset(username="", userid=0, name="Arden", startposition=5, colour="", score=0, new=False, rating=0, win=True), PlayerXMLDataset(username="", userid=0, name="Shelly", startposition=6, colour="", score=0, new=False, rating=0, win=False)] def test_read_xml_file(self): self.readxml.read_xml_file('test.xml') for x in self.attr1: play_fun1 = operator.attrgetter(x) for play_one, play_two in zip(self.readxml.plays, self.xml_data): self.assertEqual(play_fun1(play_one), play_fun1(play_two)) for y in self.attr2: play_fun2 = operator.attrgetter(y) for player_one, player_two in zip(play_one.players, play_two.players): self.assertEqual(play_fun2(player_one), play_fun2(player_two))
def read_xml(self): """ Reads all the xml files that were download by retrieve_xml :return: None """ read_xml = ReadXML() read_xml.read_xml_all(os.path.join(os.getcwd(), "plays"), self.count_to) self.current = read_xml.plays read_xml.read_xml_all(self.backup_dir, self.backup_count) self.backup = read_xml.plays for cur in self.current: for back in self.backup: if cur.id == back.id: if cur != back: self.play_nums.append(back)
class PlayerData: def __init__(self): self.pagesize = 100 self.username = "******" self.ignore = [ 'Keith', 'Paul', 'Dempsey', 'Other', 'Kelly', 'Alyssa', 'Player 6', 'Mark', 'Player 7', 'Beulah', 'Besa', 'Player 5', 'Raymon', 'Play 2', 'Jay', 'Play 6', 'Play 5', 'Play 4', 'Play 3', 'Anthony', 'Bill' ] self.url = f'http://www.boardgamegeek.com/xmlapi2/plays?username={self.username}' \ f'&pagesize={str(self.pagesize)}&page=' self.filename = "plays.xml" self.downloadXML = DownloadXML(self.url, self.filename) self.readXML = ReadXML() self.re_download = False self.count_to = BGGModule.Functions.count_to(self.username, self.pagesize) self.left_off = 1 def read(self, plays): return BGGModule.Functions.load_info(self.ignore, plays) def download_all(self): print("Download All Starting!") for i in range(1, self.count_to + 1): print(f'Downloading Plays{str(i)}') if os.path.isfile(f'plays{str(i)}.xml'): continue self.downloadXML.download(self.url + str(i), f'plays{str(i)}.xml') print("Download All Complete!") def read_all(self): print("Reading All XML files...") for i in range(1, self.count_to + 1): print(f'Reading Plays{str(i)}') self.readXML.read_xml_file(f'plays{str(i)}.xml') print("Done Reading All XML files...") def input_data(self): from DatabaseInteractions import add_record, commit print("Inputting data into database...") idx = 0 for play in self.readXML.plays: print(f'Record {play.id}') add_record(play) idx += 1 print("Done Inputting data into database...") print(f'A total of {idx} Records Inputted.') commit() def input_read(self): from DatabaseInteractions import add_record, commit print("Reading All XML files...") idx = 0 for i in range(self.left_off, self.count_to + 1): self.readXML.plays = [] print(f'Reading Plays{str(i)}') self.readXML.read_xml_file(f'plays{str(i)}.xml') for play in self.readXML.plays: print(f'Record {play.id}') add_record(play) idx += 1 commit() self.left_off = i print("Done Reading All XML files...") print(f'A total of {idx} Records Inputted.') self.left_off = 1 @staticmethod def clear(): from DatabaseInteractions import rebuild_database print('Clear Database...') rebuild_database() print('Database Clear...') def update(self, num_plays, pagesize=10): from DatabaseInteractions import add_records val = False url = self.url.replace('pagesize=100', f'pagesize={pagesize}') download = DownloadXML(url, 'update.xml') download.download() read = ReadXML() read.read_xml_file('update.xml') if read.play_count > num_plays: how_many = read.play_count - num_plays if how_many > pagesize: val = self.update(num_plays, how_many) add_records(read.plays) return val