Last active
November 27, 2018 02:55
-
-
Save jkcgs/d1a87b1e9d4744c57a2415f6268ac9e7 to your computer and use it in GitHub Desktop.
Automatically adds a Nyaa.si rss search (horriblesubs 1080p) to qBittorrent with autodownload, using the web API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
import requests | |
import feedparser | |
import json | |
from urllib.parse import quote_plus, quote | |
from os import path, listdir, sep, mkdir | |
r = requests.Session() | |
r.cookies = requests.cookies.RequestsCookieJar() | |
# this is required because qBittorrent doesn't recognize URI params like "&name=value" and stuff | |
bitly_token = '' # add a bitly generic token | |
host = '127.0.0.1' # change | |
port = 8880 # change | |
usr = 'admin' # change | |
pwd = '' # change | |
# this will be used as a base folder, then subfolders with the search text will be created | |
savepath = 'D:\\Series' # change | |
parser = argparse.ArgumentParser() | |
parser.add_argument('--name', '-n', help='series name', nargs='+', required=True) | |
parser.add_argument('--basepath', '-b', default=savepath, help='base path for downloaded files') | |
parser.add_argument('--host', '-H', default=host, help='qbittorrent api host') | |
parser.add_argument('--port', '-P', default=port, help='qbittorrent api port', type=int) | |
parser.add_argument('--username', '-u', default=usr, help='qbittorrent api username') | |
parser.add_argument('--password', '-p', default=pwd, help='qbittorrent api password') | |
parser.add_argument('--force', '-f', default=False, help='force creation if folder already exists', action='store_true') | |
args = parser.parse_args() | |
name = ' '.join(args.name) | |
dlpath = path.join(savepath, name) | |
if not args.force and path.exists(dlpath) and listdir(dlpath): | |
print('[!] Download path exists and is not empty, use --force to ignore this') | |
exit(1) | |
url = 'http://{}:{}/api/v2/'.format(args.host, args.port) | |
### Login | |
login_url = '{}auth/login'.format(url) | |
print('[~] Loading', login_url, 'user', args.username) | |
x = r.get(login_url, params={'username': args.username, 'password': args.password}) | |
if x.text.strip() != 'Ok.': | |
print('[!] Wrong credentials.') | |
exit(1) | |
else: | |
print('[+]', x.text) | |
### Check current subscriptions | |
folder_exists = False | |
items_url = '{}rss/items'.format(url) | |
print('[~] Fetching current items...') | |
print('[~] Loading', items_url) | |
x = r.get(items_url).json() | |
if 'Anime' in x: | |
folder_exists = True | |
if name in x['Anime']: | |
print('[!] Item with --name already exists') | |
exit(1) | |
### Create Anime folder in RSS subscriptions | |
cfolder_url = '{}rss/addFolder'.format(url) | |
print('[~] Creating Anime folder...') | |
print('[~] Loading', cfolder_url) | |
x = r.get(cfolder_url, params={'path': 'Anime'}) | |
if x.text.strip() != '': | |
print('[~]', x.text) | |
if not x.text.endswith('Anime.'): | |
exit(1) | |
else: | |
print('[+] Created') | |
### Check if RSS feed subscription exists | |
addfeed_url = '{}rss/addFeed'.format(url) | |
feedurl = 'https://nyaa.si/?page=rss&q=1080p+{}&c=0_0&f=0&u=HorribleSubs' | |
feedurl = feedurl.format(quote_plus(name)) | |
print('[~] Feed URL:', feedurl) | |
print('[~] Checking feed...') | |
rss = feedparser.parse(feedurl) | |
print('[~] Feed title:', rss['feed']['title']) | |
### Generate a Bitly link to be able to add it to qbt | |
bitlyurl = 'https://api-ssl.bitly.com/v4/bitlinks' | |
print('[~] Creating Bitly URL') | |
print('[~] Loading', bitlyurl) | |
x = r.post('https://api-ssl.bitly.com/v4/bitlinks', | |
headers={'Authorization': 'Bearer ' + bitly_token, 'Content-Type': 'application/json'}, | |
data=json.dumps({ | |
# 'domain': 'bit.ly', | |
'long_url': feedurl | |
}) | |
) | |
bdata = x.json() | |
if 'message' in bdata: | |
print('[!] Error:', bdata['message'], bdata['description']) | |
exit(1) | |
print('[~] Generated url:', x.json()['link']) | |
### Create feed subscription | |
feedurl = x.json()['link'] | |
print('[~] Adding RSS subscription...') | |
print('[~] Loading', addfeed_url) | |
x = r.get(addfeed_url, params='path=Anime{}{}&url={}'.format(sep, name, feedurl)) | |
print('[~]', x.text.strip() or 'Added') | |
### Create auto-download rule | |
autodl_url = '{}rss/setRule'.format(url) | |
if not path.exists(dlpath): | |
mkdir(dlpath) | |
print('[~] Creating auto-download rule'), | |
print('[~] Loading', autodl_url) | |
durl = autodl_url + '?ruleName={}&ruleDef={}'.format( | |
name, quote(json.dumps({ | |
'enabled': True, | |
'savePath': dlpath, | |
'affectedFeeds': feedurl | |
})) | |
) | |
x = r.get(durl) | |
print('[+]', x.text or 'Rule created') | |
print('[~] Ready') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment