Skip to content

Instantly share code, notes, and snippets.

@The0x539
Last active April 15, 2020 19:07
Show Gist options
  • Save The0x539/b79ae13b686d87536e9e41ffc8b34f70 to your computer and use it in GitHub Desktop.
Save The0x539/b79ae13b686d87536e9e41ffc8b34f70 to your computer and use it in GitHub Desktop.
import requests as http
import feedparser as rss
import time
from dataclasses import dataclass
# replace as necessary with a module appropriate for your use case
import deluge
# not the one on PyPI, something much simpler:
# class AD(dict): def __init__(s, *a, **k): super(AD, s).__init__(*a, **k); s.__dict__ = s
# credit to natevw on StackOverflow: https://stackoverflow.com/a/14620633
from AttrDict import AttrDict
@dataclass(init=False, repr=False, frozen=True)
class Submission:
def __init__(self, attrs):
for k in ('title', 'size', 'magnet', 'post', 'date', 'hash', 'category', 'category_id', 'num_comments'):
self.__dict__[k] = attrs[k]
self.__dict__['id'] = self.post.rpartition('/')[2]
def __str__(self):
return f'{self.title} {time.asctime(self.date)}'
def __repr__(self):
return f'{self.post} - {self.title} ({self.size})'
def download(self):
# replace as necessary with code appropriate for your use case
deluge.add(self.magnet)
def search(q='', f='0', c='0_0', user=None):
query = {'page': 'rss', 'f': f, 'c': c, 'q': q}
url = 'https://nyaa.si'
if user:
query['u'] = user
req = http.get(url, query)
req.raise_for_status()
feed = rss.parse(req.text)
items = []
for entry in feed.entries:
entry = AttrDict(entry)
attrs = AttrDict()
attrs.title = entry.title
attrs.size = entry.nyaa_size
attrs.magnet = entry.link
attrs.post = entry.id
attrs.date = entry.published_parsed
attrs.hash = entry.nyaa_infohash
attrs.category = entry.nyaa_category
attrs.category_id = entry.nyaa_categoryid
attrs.num_comments = entry.nyaa_comments
items.append(Submission(attrs))
return items
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment