Skip to content

Instantly share code, notes, and snippets.

@iGlitch
Created October 26, 2016 10:53
Show Gist options
  • Save iGlitch/c111648206b8a21c2f7eb466326715c2 to your computer and use it in GitHub Desktop.
Save iGlitch/c111648206b8a21c2f7eb466326715c2 to your computer and use it in GitHub Desktop.
nyaa torrent uploader
#!/usr/bin/env python3
import cfscrape, os ,sys, subprocess, argparse, re
from configparser import SafeConfigParser
config = SafeConfigParser()
config_file = os.path.expanduser("~") + '/.nyaa_uploader.ini'
config.read(config_file)
if not os.path.isfile(config_file):
print('Config file doesn\'t exist creating one now.')
nyaa_user = input('Nyaa username: ')
nyaa_pass = input('Nyaa password: ')
nyaa_desc = input('Default torrent description: ')
nyaa_info = input('Default torrent information: ')
config.add_section('main')
config.set('main', 'user', nyaa_user)
config.set('main', 'pass', nyaa_pass)
config.set('main', 'desc', nyaa_desc)
config.set('main', 'info', nyaa_info)
with open(config_file, 'w') as f:
config.write(f)
print('Config file written. Exiting.')
sys.exit(0)
else:
username = config.get('main', 'user')
password = config.get('main', 'pass')
desc = config.get('main', 'desc')
info = config.get('main', 'info')
parser = argparse.ArgumentParser(description='Nyaa torrent uploader.')
parser.add_argument('-f', '--file', help='path to file.', nargs=1, action='store', dest='filename', metavar='path/to/file')
parser.add_argument('-v', '--version', help='Prints version info.', action='store_true')
parser.add_argument('-u', '--username', help='Nyaa username.', nargs=1, action='store', dest='username', metavar='username', default=username)
parser.add_argument('-p', '--password', help='Nyaa password.', nargs=1, action='store', dest='password', metavar='password', default=password)
parser.add_argument('-c', '--category', choices=["raw", "sub"], help='Nyaa category. options are sub or raw', nargs=1, action='store', dest='category', metavar='category', default='raw')
parser.add_argument('-i', '--info', help='Text that goes into the torrent information field on Nyaa.', nargs=1, action='store', dest='info', metavar='\'Torrent information field on nyaa\'', default=info)
parser.add_argument('-d', '--description', help='Text that goes into the torrent description field on Nyaa.', nargs=1, action='store', dest='desc', metavar='\'Torrent description field on nyaa\'', default=desc)
parser.add_argument('-m', '--move', help='Move file to directory after completed uploading.', nargs=1, action='store', dest='movedir', metavar='move_directory')
parser.add_argument('-t', '--torrentmove', help='Move torrent to directory after completed uploading.', nargs=1, action='store', dest='torrentmovedir', metavar='torrent_move_directory')
args = parser.parse_args()
if args.version:
print('Version 0.01 by glitch')
sys.exit(0)
def nyaa_cats(category):
return {
'raw': '1_11',
'sub': '1_37'}[category]
def isabs(path):
if not os.path.isabs(path):
abs_path = os.getcwd() + '/' + path
else:
abs_path = path
return abs_path
if not (args.filename):
parser.error('No file provided, use -f')
filename = isabs(''.join(args.filename))
if args.torrentmovedir:
torrentmovedir = isabs(''.join(args.torrentmovedir))
if not os.path.isdir(torrentmovedir):
print('Can\'t move torrent to ' + torrentmovedir + ' directory doesn\'t exist. Exiting.')
sys.exit(1)
if args.movedir:
movedir = isabs(''.join(args.movedir))
if not os.path.isdir(movedir):
print('Can\'t move file to ' + movedir + ' directory doesn\'t exist. Exiting.')
sys.exit(1)
torrentfile = filename + '.torrent'
scraper = cfscrape.create_scraper()
def login(scraper):
try:
scraper.get('https://www.nyaa.se/?page=login')
if not 'Login successful!' in scraper.post('https://www.nyaa.se/?page=login', data={'method': '1', 'login': ''.join(args.username), 'password': ''.join(args.password)}).text:
print('Couldn\'t login to Nyaa.')
sys.exit(1)
except:
print('Error while attempting to login to Nyaa.')
sys.exit(1)
def get_tid(ul_txt):
tid_re = r'<a href="//www\.nyaa\.se\/\?page=view&#38;tid=(\d+)">View your torrent\.<\/a>'
tid_se = re.search(tid_re, ul_txt.text)
return int(tid_se.group(1))
def move_torrent(torrentmovedir, torrentfile):
try:
os.rename(torrentfile, torrentmovedir + '/' + os.path.basename(torrentfile))
except:
print('Couldn\'t move torrent file to new directory. Check permissions?')
def move_file(movedir, filename):
try:
os.rename(filename, movedir + '/' + os.path.basename(filename))
except:
print('Couldn\'t move file to new directory. Check permissions?')
def upload(scraper, filename, torrentfile):
if not os.path.isfile(filename):
print('File doesn\'t exist can\'t create torrent.')
sys.exit(1)
try:
subprocess.call(['mktorrent', '-l', '18', '-a', '\"http://open.nyaatorrents.info:6544/announce\"', '-a', '\"udp://tracker.openbittorrent.com:80\"', filename])
except:
print('Couldn\'t make torrent file.')
sys.exit(1)
if not os.path.isfile(torrentfile):
print('Torrent file doesn\'t exist exiting.')
sys.exit(1)
try:
torrent = {'torrent': open(torrentfile, 'rb')}
scraper.get('https://www.nyaa.se/?page=upload')
ul = scraper.post('https://www.nyaa.se/?page=upload', files=torrent, data={'name': '', 'torrenturl': '', 'catid': nyaa_cats(args.category), 'info': ''.join(args.info), 'description': ''.join(args.desc), 'rules': '1', 'submit': 'Upload'})
if ul.status_code == int(200):
return ul
else:
print('Got an error while uploading to Nyaa: {0}. Exiting.'.format(ul.status_code))
sys.exit(1)
except:
print('Couldn\'t upload torrent to Nyaa.')
sys.exit(1)
login(scraper)
ul_txt = upload(scraper, filename, torrentfile)
tid = get_tid(ul_txt)
url = "http://www.nyaa.se/?page=view&tid={0}".format(tid)
print(url)
if args.torrentmovedir:
move_torrent(torrentmovedir, torrentfile)
if args.movedir:
move_file(movedir, filename)
@iGlitch
Copy link
Author

iGlitch commented Oct 26, 2016

want to get the info_hash from a bunch of files loaded in transmission

sudo find /var/lib/transmission-daemon/info/torrents/ -regex '.(Yukikaze|snowfag).' -exec transmission-show -m {} ; | awk -F'[:&]' '{ print $4 }'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment