Skip to content

Instantly share code, notes, and snippets.

@GINK03
Last active April 5, 2020 05:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GINK03/b0afe4b74026a68bc8f5f7d45ed3598e to your computer and use it in GitHub Desktop.
Save GINK03/b0afe4b74026a68bc8f5f7d45ed3598e to your computer and use it in GitHub Desktop.
2020-04-05-torrent
from subprocess import PIPE
from subprocess import Popen
import re
import requests
from bs4 import BeautifulSoup
import re
import shlex
from pathlib import Path
from collections import namedtuple
from concurrent.futures import ThreadPoolExecutor
from os import environ as E
import os
HERE = Path(__file__).resolve().parent
Datum = namedtuple('Datum', ['id', 'name', 'magnet'])
def nyaanet():
data = []
for page in range(1, 10):
url = f"https://nyaa.net/search/{page}?c=4_8&order=false&sort=2"
with requests.get(url) as r:
html = r.text
soup = BeautifulSoup(html, 'lxml')
for tr in soup.find_all('tr', {'class': 'torrent-info'}):
id = tr.get('id')
name = tr.find('td', {'class': 'tr-name'}).text.strip()
magnet = tr.find(attrs={'class': 'tr-links'}).find('a', attrs={'href': re.compile(r'^magnet:')}).get('href')
datum = Datum(id, name, magnet)
data.append(datum)
return data
def nyaasi():
data = []
for page in range(1, 10):
url = f"https://nyaa.si/?c=3_3&s=seeders&o=desc&p={page}"
with requests.get(url) as r:
html = r.text
# print(html)
soup = BeautifulSoup(html, 'lxml')
for tr in soup.find_all('tr', attrs={'class': 'default'}):
name = ''.join([a.text for a in tr.find_all('a', {'href': True})])
id = tr.find('a', {'href': re.compile(r'^/view/[0-9]{1,}$')}).get('href')
magnet = tr.find('a', {'href': re.compile(r'^magnet:')}).get('href')
datum = Datum(id, name.strip(), magnet.strip())
data.append(datum)
return data
def proc(datum):
pid = os.getpid()
if Path(f'{HERE}/Downloads/{datum.name}').exists():
return
with Popen(['aria2c', datum.magnet, '--dir', f'{HERE}/Downloads/{datum.name}', '--seed-time=0', '--dht-listen-port=63000-65535', '--listen-port=60000-62999'], stdout=None) as proc:
try:
proc.communicate(timeout=60 * 60)
except Exception as exc:
print(exc)
def run():
data = nyaanet() + nyaasi()
if E.get('SINGLE') == 'TRUE':
for datum in data:
proc(datum)
else:
with ThreadPoolExecutor(max_workers=1000) as exe:
exe.map(proc, data)
if __name__ == "__main__":
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment