async def nhsearch(self, ctx, *, query): """Issues a search to nhentai with <query>. Displays a random doujin selected from the search results.""" page = random.randint(1, 10) results = nhentai.search(query, page) if not results: results = nhentai.search(query, 1) try: doujin = random.choice(results) e = ArgentaEmbed(ctx.author, title=doujin.titles['english'], url=doujin.url) e.add_field(name="Magic number", value=doujin.id) e.add_field(name="Tags", value=', '.join([i.name for i in doujin.tags])) e.set_image(url=doujin.cover) e.colour = discord.Colour.teal() log.info("Doujin search requested.") await ctx.reply(embed=e) except ValueError: log.info(f"Requested: {query}. Doujin not found.") await ctx.reply(f"Doujinshi not found with query {query}") except IndexError: log.info(f"Requested: {query}. Doujin not found.") await ctx.reply(f"Doujinshi not found with query {query}")
async def search(self, ctx, query, pageno=1): if ctx.author.id in data['bannedids']: await ctx.send( ':no_entry_sign: **Sorry, but you are banned from using Nen!**\n **Please contact the owner on the official server to appeal for unban**' ) return doujinlist = [] doujinlist.extend(nh.search(query=query, page=1, sort_by='popular')) doujinlist.extend(nh.search(query=query, page=2, sort_by='popular')) partitions = int(len(doujinlist) / 5) titlegroups = [] for i in range(partitions): i += 1 start = (i * 5) - 5 end = i * 5 titlegroup = [] for index in range(len(doujinlist)): if index >= start and index < end: doujin = doujinlist[index] title = doujin.titles["pretty"] titlemsg = f'`{title}: `**`{doujin.id}`**\n' titlegroup.append(titlemsg) titlegroups.append(titlegroup) i -= 1 if len(titlegroups) == 0: await ctx.send('`No results where found, senpai!`') else: embed = Embed(title=f'Search Results for: \"{query}\"', color=Color.darker_gray()) embed.set_footer( text= f'Requested by {ctx.author.name}#{ctx.author.discriminator}, Page: {pageno}', icon_url=ctx.author.avatar_url) titlegroup = titlegroups[pageno] titlemsg = '' for title in titlegroup: titlemsg += title embed.description = f'{titlemsg}' await ctx.send(embed=embed)
def lang(parameter): try: if parameter[0].lower() == "en": parameter[0] = "english" if parameter[0].lower() == "jp": parameter[0] = "japanese" if parameter[0].lower() == "cn": parameter[0] = "chinese" if not parameter[0].lower() in ["english", "japanese", "chinese"]: raise ValueError( "Parameter value does not match with existing option") randPage = random.randint(1, 20) randDoujin = random.randint(0, 24) print(randPage) doujin_list = nhentai.search(parameter[0], randPage, "popular-today") doujin = nhDetail(doujin_list[randDoujin]) embed = doujin_embed(doujin) return embed except Exception as e: return error_embed(f"{type(e).__name__}: {e}.", "rand_lang(param_type, parameter)")
def search_ndoujin(query: str): nid = 1 if not query: nid = nhentai.get_random_id() elif query.isnumeric(): nid = int(query) else: nid = choice(nhentai.search(query=query)).id return nhentai.get_doujin(id=nid)
async def nhsearch(self, ctx, *, query): """Issues a search to nhentai with <query>. Displays a random doujin selected from the search results.""" page = random.randint(1, 10) results = [d for d in nhentai.search(query, page)] if not results: results = [d for d in nhentai.search(query, 1)] try: d = random.choice(results) url = f"http://nhentai.net/g/{d.magic}" e = discord.Embed(title=d.name, url=url) e.add_field(name="Magic number", value=d.magic) e.add_field(name="Tags", value=', '.join(d.tags)) e.set_image(url=d.cover) e.colour = discord.Colour.teal() log.info("Doujin search requested.") await ctx.send(embed=e) except errors.DoujinshiNotFound: log.info(f"Requested: {query}. Doujin not found.") await ctx.send(f"Doujinshi not found with query {query}") except IndexError: log.info(f"Requested: {query}. Doujin not found.") await ctx.send(f"Doujinshi not found with query {query}")
async def nhentai(cmd, message, args): search = ' '.join(args) try: n = 0 list_text = '```' for entry in nh.search(search)['result']: n += 1 list_text += '\n#' + str(n) + ' ' + entry['title']['pretty'] if len(nh.search(search)['result']) > 1: await cmd.bot.send_message(message.channel, list_text + '\n```') choice = await cmd.bot.wait_for_message(author=message.author, channel=message.channel, timeout=20) await cmd.bot.send_typing(message.channel) try: nh_no = int(choice.content) - 1 except: await cmd.bot.send_message(message.channel, 'Not a number or timed out... Please start over') return else: nh_no = 0 if nh_no > len(nh.search(search)['result']): await cmd.bot.send_message(message.channel, 'Number out of range...') else: hen_name = nh.search(search)['result'][nh_no]['title']['pretty'] hen_id = nh.search(search)['result'][nh_no]['id'] hen_media_id = nh.search(search)['result'][nh_no]['media_id'] hen_url = ('https://nhentai.net/g/' + str(hen_id) + '/') hen_img = ('https://i.nhentai.net/galleries/' + str(hen_media_id) + '/1.jpg') nhen_text = '' nh_cover_raw = requests.get(hen_img).content nh_cover_res = Image.open(BytesIO(nh_cover_raw)) nh_cover = nh_cover_res.resize((251, 321), Image.ANTIALIAS) base = Image.open(cmd.resource('img/base.png')) overlay = Image.open(cmd.resource('img/overlay_nh.png')) base.paste(nh_cover, (100, 0)) base.paste(overlay, (0, 0), overlay) base.save('cache/ani/nh_' + message.author.id + '.png') for tags in nh.search(search)['result'][nh_no]['tags']: nhen_text += '[' + str(tags['name']).title() + '] ' await cmd.bot.send_file(message.channel, 'cache/ani/nh_' + message.author.id + '.png') await cmd.bot.send_message(message.channel, 'Name:\n```\n' + hen_name + '\n```\nTags:\n```\n' + nhen_text + '\n```\nBook URL: <' + hen_url + '>') os.remove('cache/ani/nh_' + message.author.id + '.png') except nh.nhentai.nHentaiException as e: await cmd.bot.send_message(message.channel, e)
def tag(parameter): try: randPage = random.randint(1, 20) randDoujin = random.randint(0, 24) doujin_list = nhentai.search(parameter, randPage, "popular-today") doujin = nhDetail(doujin_list[randDoujin]) embed = doujin_embed(doujin) return embed except IndexError as e: return error_embed( f"{type(e).__name__}: {e}. (include less tag if this error persist)", "rand_lang(param_type, parameter)") except Exception as e: return error_embed(f"{type(e).__name__}: {e}.", "rand_lang(param_type, parameter)")
async def gib(ctx, *, args): if args.isdigit(): try: await ctx.send("processing code...") doujin=nhentai.get_doujin(int(args)) thumb=doujin.thumbnail name=doujin.titles['english'] res = [] for i in doujin.tags: if i[0] not in res and (i != ''): res.append(i[2]) await ctx.send(name+"\n"+thumb+"\n"+str(res)) except Exception as e: logging.error("gib code failed: %s",e) await ctx.send("unable to fulfil request") elif isinstance(args,str): try: await ctx.send("processing tag...") o=1 ret=[] tag=1 while tag==1: listo=nhentai.search(args,o) if listo==[]: tag=0 for i in listo: ret.append(str(i.id)) o=o+1 key=random.randint(0,len(ret)-1) doujin=nhentai.get_doujin(int(ret[key])) name=doujin.titles['english'] thumb=doujin.thumbnail res = [] for i in doujin.tags: if i[0] not in res and (i != ''): res.append(i[2]) await ctx.send(name+"\n"+thumb+"\n"+ret[key]+"\n"+str(res)) except Exception as e: await ctx.send("unable to fulfil request") logging.error("gib tag failed: %s",e) else: await ctx.send("WTF are you asking for?")
async def hentai(cmd, message, args): search = ' '.join(args) if search == '': await message.channel.send(cmd.help()); return try: n = 0 list_text = '```' entries = nhentai.search(search)['result'] if len(entries) <= 1: nh_no = 0 else: for entry in entries: n += 1 list_text += '\n#' + str(n) + ' ' + entry['title']['pretty'] try: list_message = await message.channel.send(list_text + '\n```\nPlease type the number corresponding to the anime of your choice `(1 - ' + str(len(entries)) + ')`') except: await message.channel.send('The list is way too big, please be more specific...'); return try: choice = await cmd.bot.wait_for(event='message', check=lambda m: m.author == message.author ,timeout=20) try: await list_message.delete() except: pass try: await choice.delete() except: pass except Exception: try: await list_message.delete() except: pass await message.channel.send('timed out... Please start over'); return try: nh_no = int(choice.content) - 1 except: await cmd.bot.send_message(message.channel, 'Not a number or timed out... Please start over') return try: nh_no = int(choice.content) - 1 except: await message.channel.send('Invalid choice... Please start over'); return if nh_no < 0 or nh_no > len(entries) - 1: await message.channel.send('Invalid choice... Please start over'); return hen_name = entries[nh_no]['title']['pretty'] hen_id = entries[nh_no]['id'] hen_media_id = entries[nh_no]['media_id'] hen_url = ('https://nhentai.net/g/' + str(hen_id) + '/') hen_img = ('https://i.nhentai.net/galleries/' + str(hen_media_id) + '/1.jpg') nhen_text = '' async with aiohttp.ClientSession() as session: async with session.get(hen_img) as data: nh_cover_raw = await data.read() nh_cover_res = Image.open(BytesIO(nh_cover_raw)) nh_cover = nh_cover_res.resize((251, 321), Image.ANTIALIAS) base = Image.open(cmd.resource('img/base.png')) overlay = Image.open(cmd.resource('img/overlay_nh.png')) base.paste(nh_cover, (100, 0)) base.paste(overlay, (0, 0), overlay) base.save('cache/nh_' + str(message.author.id) + '.png') for tags in entries[nh_no]['tags']: nhen_text += '[' + str(tags['name']).title() + '] ' await message.channel.send(file=discord.File('cache/nh_' + str(message.author.id) + '.png')) await message.channel.send('Name:\n```\n' + hen_name + '\n```\nTags:\n```\n' + nhen_text + '\n```\nBook URL: <' + hen_url + '>') os.remove('cache/nh_' + str(message.author.id) + '.png') except nhentai.nhentai.nHentaiException as e: await message.channel.send(e)
def _str_search(self, query: str): """Query using a string""" return [*_nhentai.search(query)]
async def on_message(self, message, pfx): if message.content.startswith(pfx + 'nhentai'): await self.client.send_typing(message.channel) cmd_name = 'NHentai' try: self.log.info( 'User %s [%s] on server %s [%s], used the ' + cmd_name + ' command on #%s channel', message.author, message.author.id, message.server.name, message.server.id, message.channel) except: self.log.info( 'User %s [%s], used the ' + cmd_name + ' command.', message.author, message.author.id) try: ch_id = str(message.channel.id) query = 'SELECT PREMITTED FROM NSFW WHERE CHANNEL_ID=?' perms = self.db.execute(query, ch_id) permed = 'No' for row in perms: permed = row[0] except DatabaseError: permed = 'No' except SyntaxError: permed = 'No' if permed == 'Yes': permitted = True else: permitted = False # End Perms if permitted is True: # 251 x 321 search = message.content[len(pfx) + len('nhentai') + 1:] try: n = 0 list_text = '```' for entry in nh.search(search)['result']: n += 1 list_text += '\n#' + str( n) + ' ' + entry['title']['pretty'] if len(nh.search(search)['result']) > 1: await self.client.send_message(message.channel, list_text + '\n```') choice = await self.client.wait_for_message( author=message.author, channel=message.channel, timeout=20) await self.client.send_typing(message.channel) try: nh_no = int(choice.content) - 1 except: await self.client.send_message( message.channel, 'Not a number or timed out... Please start over' ) return else: nh_no = 0 if nh_no > len(nh.search(search)['result']): await self.client.send_message( message.channel, 'Number out of range...') else: hen_name = nh.search( search)['result'][nh_no]['title']['pretty'] hen_id = nh.search(search)['result'][nh_no]['id'] hen_media_id = nh.search( search)['result'][nh_no]['media_id'] hen_url = ('https://nhentai.net/g/' + str(hen_id) + '/') hen_img = ('https://i.nhentai.net/galleries/' + str(hen_media_id) + '/1.jpg') nhen_text = '' nh_cover_raw = requests.get(hen_img).content nh_cover_res = Image.open(BytesIO(nh_cover_raw)) nh_cover = nh_cover_res.resize((251, 321), Image.ANTIALIAS) base = Image.open('img/ani/base.png') overlay = Image.open('img/ani/overlay_nh.png') base.paste(nh_cover, (100, 0)) base.paste(overlay, (0, 0), overlay) base.save('cache\\ani\\nh_' + message.author.id + '.png') for tags in nh.search(search)['result'][nh_no]['tags']: nhen_text += '[' + str(tags['name']).title() + '] ' await self.client.send_file( message.channel, 'cache\\ani\\nh_' + message.author.id + '.png') await self.client.send_message( message.channel, 'Name:\n```\n' + hen_name + '\n```\nTags:\n```\n' + nhen_text + '\n```\nBook URL: <' + hen_url + '>') os.remove('cache\\ani\\nh_' + message.author.id + '.png') except nh.nhentai.nHentaiException as err: await self.client.send_message(message.channel, str(err)) except: await self.client.send_message( message.channel, 'Nothing found or something broke...') else: await self.client.send_message( message.channel, 'This channel does not have the NSFW Module permitted!')