def load(cls, id): """Parse content of PGI info page, e.g. http://jaskiniepolski.pgi.gov.pl/Details/Information/406""" data_path = cls.DATA_PATH.format(id=id) if not os.path.exists(data_path): cls._preload_html(id) time.sleep(2) with open(data_path) as file: doc = lxml.html.parse(file) data = OrderedDict() for e in doc.xpath('//tr'): assert len(e.getchildren()) == 2 key, value = e.getchildren() key = key.text_content() paragraphs = value.xpath('.//p') if paragraphs: value = '\n\n'.join( [clear_text(e.text_content()) for e in paragraphs]) else: value = clear_text(value.text_content()) data[clear_text(key)] = value # Read attachments file.seek(0) text = file.read() if id in PRELOAD_IMAGES: attachments = list( map(int, re.findall('showImageInfo\((\d+)\)', text))) logging.info('Preloading %d images for %s', len(attachments), data['Nazwa']) else: attachments = None # Get Links links = [ Link( 'Pokaż oryginał', f"http://jaskiniepolski.pgi.gov.pl/Details/Information/{id}" ) ] if 'geostanowiska.pgi.gov.pl' in text: geostanowisko = re.search( r'https?://geostanowiska.pgi.gov.pl/[^"\']+', text) assert geostanowisko # print(geostanowisko, geostanowisko.group()) links.append(Link('Geostanowisko', geostanowisko.group())) for content in DATA.get(id, []): if isinstance(content, Link): links.append(content) return PGIRecord(id=id, description=data, attachments=attachments, links=links)
def get_formatted(): with DATA.lock: init = DATA.get()[KEY] header = "**Initiative**\n" roll_list = [(init[name], name) for name in init] roll_list.sort(reverse=True) contents = "\n".join(f"{roll}: {name}" for roll, name in roll_list) return header + contents
def reset(): with DATA.lock: DATA.get()[KEY] = {} DATA.write()
def add(name: str, value: int): with DATA.lock: DATA.get()[KEY][name] = value DATA.write()
def fail(quest_num: int) -> None: with DATA.lock: index = quest_num - 1 _, quest = DATA.get()[KEY][index] DATA.get()[KEY][index] = (FAILED, quest) DATA.write()
def uncheck(quest_num: int) -> None: with DATA.lock: index = quest_num - 1 _, quest = DATA.get()[KEY][index] DATA.get()[KEY][index] = (UNCHECKED, quest) DATA.write()
def add(quest: str) -> None: with DATA.lock: DATA.get()[KEY].append((UNCHECKED, quest)) DATA.write()
def remove(quest_num: int) -> None: with DATA.lock: index = quest_num - 1 del DATA.get()[KEY][index] DATA.write()
def get() -> List[Tuple[int, str]]: with DATA.lock: return DATA.get()[KEY]
async def server(ctx): """ Gives a link to the Foundry server. """ with DATA.lock: await ctx.send(DATA.get()["server"])
@bot.command() async def server(ctx): """ Gives a link to the Foundry server. """ with DATA.lock: await ctx.send(DATA.get()["server"]) async def on_command_error(ctx, error): print(error) mention = ctx.author.mention if ctx.command: usage = usage_string(ctx.command) help = ctx.command.help await ctx.send(f"{mention}\n{usage}\n\n{help}") else: await ctx.send(f"{mention}\n{error}") def usage_string(command) -> str: return f"usage: `>{command.name} {command.signature}`" bot.on_command_error = on_command_error with DATA.lock: api_key = DATA.get()["api-key"] bot.run(api_key)
def get_dark_light(): with DATA.lock: d = DATA.get()[KEY] return d["dark"], d["light"]
def set_dark_light(dark, light): with DATA.lock: DATA.get()[KEY]["dark"] = dark DATA.get()[KEY]["light"] = light DATA.write()