def __init__(self, app): BaseHttpHandler.__init__(self) self.app = app self.parser = AbandonGameParser() self._base_url = 'http://www.abandonia.com' self._cachedir = self.app.datadir / 'cache' self._htmldir = self._cachedir / 'html' self._gamedir = self._cachedir / 'games' self._extrasdir = self._cachedir / 'extras' for adir in [self._htmldir, self._gamedir, self._extrasdir]: if not adir.exists(): adir.mkdir()
class AbandonGamesHandler(BaseHttpHandler): def __init__(self, app): BaseHttpHandler.__init__(self) self.app = app self.parser = AbandonGameParser() self._base_url = 'http://www.abandonia.com' self._cachedir = self.app.datadir / 'cache' self._htmldir = self._cachedir / 'html' self._gamedir = self._cachedir / 'games' self._extrasdir = self._cachedir / 'extras' for adir in [self._htmldir, self._gamedir, self._extrasdir]: if not adir.exists(): adir.mkdir() def get_game_id(self, url): return int(url.split('/en/games/')[1].split('/')[0]) def _htmlfilename(self, gameid): fname = 'game-%d.html' % gameid return self._htmldir / fname def is_game_downloaded(self, gameid): fname = path('%s-nogame' % self._htmlfilename(gameid)) if fname.exists(): return False else: return True def game_download_path(self, filename): return self._gamedir / filename def does_download_exist(self, filename): game = self.game_download_path(filename) return game.exists() def set_game_downloaded(self, gameid): fname = path('%s-nogame' % self._htmlfilename(gameid)) os.remove(fname) def extras_path(self, filename=''): return self._extrasdir / filename def is_extra_downloaded(self, filename): epath = self.extras_path(filename=filename) return epath.exists() def get_game_data(self, gameid): self.parser.clear_data() print gameid, type(gameid), 'in get_game_data' self.parser.gameid = gameid data = file(self._htmlfilename(gameid)).read() self.parser.feed(data) self.parser.gameid = gameid def _get_html_file(self, url, filename): data = self.perform_request(url).read() if data: outfile = file(filename, 'w') outfile.write(data) outfile.close() else: raise ValueError, "empty data for %s" % url def handle_url(self, url): gameid = self.get_game_id(url) html = self._htmlfilename(gameid) data = '' if not html.exists(): self._get_html_file(url, html) dl = '%s-nogame' % html # make an empty file that will be removed # when game is downloaded file(dl, 'w') self.get_game_data(gameid) def get_dl_link(self): print self._base_url, self.parser.download_link dl_link = self._base_url + self.parser.download_link print dl_link response = self.perform_request(dl_link) self._dlr = response.read() parser = AbandonDownloadParser() parser.feed(str(self._dlr)) self.real_download_link = parser.download_link def search_for_game(self, text): # replace spaces with +'s search_term = '+'.join(text.split()) search_url = '%s/en/search_abandonia/%s' % (self._base_url, search_term) response = self.perform_request(search_url) def get_all_html_ids(self): ls = self._htmldir.files('game-*.html') gameids = [] for f in ls: gameid = int(f.split('game-')[1].split('.')[0]) gameids.append(gameid) gameids.sort() return gameids
parser.feed(str(self._dlr)) self.real_download_link = parser.download_link def search_for_game(self, text): # replace spaces with +'s search_term = '+'.join(text.split()) search_url = '%s/en/search_abandonia/%s' % (self._base_url, search_term) response = self.perform_request(search_url) def get_all_html_ids(self): ls = self._htmldir.files('game-*.html') gameids = [] for f in ls: gameid = int(f.split('game-')[1].split('.')[0]) gameids.append(gameid) gameids.sort() return gameids if __name__ == '__main__': m = AbandonGameParser() w = file('dunetest.html').read() m.feed(w) h = AbandonGamesHandler() u = 'http://www.abandonia.com/en/games/383' u = 'http://www.abandonia.com/en/games/593/'