def _parse_pob(author, content, minify=False): """ Trigger the parsing of the pastebin link, pass it to the output creating object and send a message back :param minify: show minified version of the embed :param content: of the message :param author: user sending the message :return: Embed """ paste_keys = pastebin.fetch_paste_key(content) if paste_keys: paste_key = random.choice(paste_keys) log.info(f"Parsing pastebin with key={paste_key} from author={author}") raw_data = pastebin.get_as_xml(paste_key) if not raw_data: log.error(f"Unable to obtain raw data for pastebin with key {paste_key}") return xml = pastebin.decode_to_xml(raw_data) if not xml: log.error(f"Unable to obtain xml data for pastebin with key {paste_key}") return web_poe_token = util.fetch_xyz_pob_token(raw_data) build = pob_xml_parser.parse_build(xml) try: embed = pob_output.generate_response(author, build, minified=minify, pastebin_key=paste_key, non_dps_skills=poe_consts, web_poe_token=web_poe_token) log.debug(f"embed={embed}; thumbnail={embed.thumbnail}") return embed except Exception as e: ex_msg = ''.join(traceback.format_exception(etype=type(e), value=e, tb=e.__traceback__)) log.error(f"Could not parse pastebin={paste_key} - Exception={ex_msg}")
async def on_message(self, message): """ Handle message events :param message: received :return: None """ react_to_dms = isinstance(message.channel, discord.abc.PrivateChannel) and self.allow_pming if message.author.bot: return if 'help' in message.content.lower() and react_to_dms: await message.channel.send("Paste your pastebin here for a quick overview or use '!pob <pastebin>' for a " "detailed response.") return if (react_to_dms or message.channel.name in self.active_channels) \ and not util.starts_with("!pob", message.content[:4]) \ and "pastebin.com/" in message.content: # check if valid xml # send message log.debug(f"A| {message.channel}: {message.content}") try: embed = self._parse_pob(message.author, message.content, minify=True) if embed: await message.channel.send(embed=embed) except HTTPError as err: log.error(f"Pastebin: Invalid pastebin-url msg={err}") except pastebin.CaptchaError as err: log.error(f"Pastebin: Marked as spam msg={err}") await message.channel.send(err.message)
def fetch_pastebin(key: str): xml = pastebin.decode_to_xml(pastebin.get_as_xml(key)) tree = ET.ElementTree(xml) file = open( get_test_path( f"in/pastebin_xmls/{PastebinHelper.to_file_name(key)}.xml"), "wb") tree.write(file) log.debug(f"Wrote content for '{key}'") file.close()
def decode_to_xml(enc, encoding='windows-1252'): enc = enc.replace("-", "+").replace("_", "/") xml_str = decode_base64_and_inflate(enc) log.debug(f"XML={xml_str}") xml = None try: xml = ET.fromstring(xml_str.decode(encoding)) except TypeError as err: log.debug(f"Could not parse the pastebin as xml msg={err}") return xml
def fetch_pastebins(content: [str]): for line in content: line = line.strip() if "pastebin.com" in line: key = line.split("/")[-1] log.debug(f"Writing content for '{line}'") xml = pastebin.decode_to_xml(pastebin.get_as_xml(key)) tree = ET.ElementTree(xml) tree.write( open( get_test_path( f"in/pastebin_xmls/{PastebinHelper.to_file_name(key)}.xml" ), "wb"))
def _generate_embed(web_poe_token, xml, author, paste_key, minify=False): if xml: build = pob_xml_parser.parse_build(xml) try: embed = pob_output.generate_response( author, build, minified=minify, pastebin_key=paste_key, non_dps_skills=poe_consts, web_poe_token=web_poe_token) log.debug(f"embed={embed}; thumbnail={embed.thumbnail}") return embed except Exception as e: ex_msg = ''.join( traceback.format_exception(etype=type(e), value=e, tb=e.__traceback__)) log.error( f"Could not parse pastebin={paste_key} - Exception={ex_msg}" )
def shrink_tree_url(tree): """ Shrink url with poeurl :param tree: :return: valid poeurl if possible else raise a value error """ # sanitize tree = tree.strip() # build requesturl param = f'{{"url":"{tree}"}}' url = f'http://poeurl.com/api/?shrink={param}' log.debug(f"Poeurl payload={url}") contents = urllib.request.urlopen(url).read().decode('utf-8') log.debug(f"Poeurl contents={contents}") contents = json.loads(contents) log.debug(f"Got json content from poeurl ... {contents}") if contents['url']: return f"http://poeurl.com/{contents['url']}" else: raise ValueError("Unable to retrieve URL")
def get_as_xml(paste_key): raw_url = 'https://pastebin.com/raw/' + paste_key log.debug(f"Retrieved from raw_url={raw_url}") data = get_raw_data(raw_url) return data