From e88ef349c4479956bb7dd9c38e9c3e99045cc2d5 Mon Sep 17 00:00:00 2001 From: Andrey <50486086+DarkCat09@users.noreply.github.com> Date: Tue, 20 Jul 2021 16:20:35 +0400 Subject: [PATCH] Add files via upload --- Procfile | 1 + getp2pbot.py | 75 ++++++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 31 ++++++++++++++++++++ runtime.txt | 1 + 4 files changed, 108 insertions(+) create mode 100644 Procfile create mode 100644 getp2pbot.py create mode 100644 requirements.txt create mode 100644 runtime.txt diff --git a/Procfile b/Procfile new file mode 100644 index 0000000..c09ed51 --- /dev/null +++ b/Procfile @@ -0,0 +1 @@ +worker: python getp2pbot.py diff --git a/getp2pbot.py b/getp2pbot.py new file mode 100644 index 0000000..b74862c --- /dev/null +++ b/getp2pbot.py @@ -0,0 +1,75 @@ +import os +import re +import requests +import discord +from discord.ext import commands +from lxml import html + +# Add the bot: +# https://discord.com/api/oauth2/authorize?client_id=866933457967513630&permissions=2147585024&scope=bot + +# Setting Up +token = 'ODY2OTMzNDU3OTY3NTEzNjMw.YPZwsw.MFEC2UJt1rJ3wvFPYB1qxRDNIzo' +bot = commands.Bot(command_prefix='/') + +@bot.command() +async def rutor(ctx, *args): + regex = re.compile(r'\'([\wА-Яа-я\s]+)\'\s*?(\d+)*$') + parsed_args = regex.match(' '.join(args)) + query = parsed_args.group(1) + try: + rownum = int(parsed_args.group(2)) + except TypeError: + rownum = 0 + + page = requests.get(f'http://rutor.info/search/{query}') + page_tree = html.fromstring(page.content) + table = page_tree.xpath('//div[@id="index"]')[0] + rows = table.xpath('.//tr[@class="gai" or @class="tum"]') + + if rownum != 0: + # The 2nd cell (index=1) is the main text + # with a link to download and a title, + # the 1st anchor (index=0) is a download link + cells = rows[rownum-1].xpath('.//td') + dllink = cells[1].xpath('.//a//@href')[0] + + linkparts = dllink.split('/') + torrentid = int(linkparts[len(linkparts)-1]) + filename = f'{torrentid}.torrent' + + torrent = requests.get(dllink).content + dlfile = open(filename, 'wb') + dlfile.write(torrent) + dlfile.close() + + hascomments = len(cells) > 4 + size = cells[3 if hascomments else 2].text + title = cells[1].xpath('.//a')[2].text.strip().replace('\'', '') + + await ctx.send( + f'`{torrentid}: {title} ({size})`', + file=discord.File(filename) + ) + os.remove(filename) + else: + choose_text = '' + i = 1 + for row in rows: + if (i > 10): + break + # The 3rd anchor (index=2) in the 2nd cell (index=1) is a title, + # the 4th cell (index=3) contains the size of a film + cells = row.xpath('.//td') + maincell = cells[1] + + hascomments = len(cells) > 4 + size = cells[3 if hascomments else 2].text + title = maincell.xpath('.//a')[2].text.strip().replace('\'', '') + + choose_text += f'${i} {title} \'{size}\'\n' + i += 1 + await ctx.send(f'Choose torrent:```bash\n{choose_text}```') + +# Starting +bot.run(token) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..c5ef244 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,31 @@ +aiohttp==3.7.4.post0 +anyio==3.2.1 +async-timeout==3.0.1 +attrs==21.2.0 +beautifulsoup4==4.9.3 +certifi==2020.12.5 +cffi==1.14.5 +chardet==4.0.0 +click==7.1.2 +discord.py==1.7.3 +h11==0.12.0 +httpcore==0.13.6 +httpx==0.18.2 +idna==2.10 +lxml==4.6.2 +multidict==5.1.0 +numpy==1.20.2 +Pillow==8.2.0 +pycparser==2.20 +pygal==2.4.0 +pygame==2.0.1 +PyNaCl==1.4.0 +PySocks==1.7.1 +requests==2.25.1 +rfc3986==1.5.0 +six==1.15.0 +sniffio==1.2.0 +soupsieve==2.2.1 +typing-extensions==3.10.0.0 +urllib3==1.26.3 +yarl==1.6.3 diff --git a/runtime.txt b/runtime.txt new file mode 100644 index 0000000..396db58 --- /dev/null +++ b/runtime.txt @@ -0,0 +1 @@ +python-3.9.6 \ No newline at end of file