diff options
author | Matt Lyon <matthewlyon18@gmail.com> | 2019-11-02 17:06:18 +1100 |
---|---|---|
committer | Matt Lyon <matthewlyon18@gmail.com> | 2019-11-02 17:06:18 +1100 |
commit | ae9e8c258acb277eb10220c0ff3a803091efa923 (patch) | |
tree | a4d39149f2d3946e88e8d26612729232f1de40fa /tpblite | |
parent | 1b3b031e4b15f947c539ae76fc892874de03c4be (diff) | |
download | tpb-lite-ae9e8c258acb277eb10220c0ff3a803091efa923.tar.gz tpb-lite-ae9e8c258acb277eb10220c0ff3a803091efa923.tar.bz2 tpb-lite-ae9e8c258acb277eb10220c0ff3a803091efa923.zip |
added utils and constants
Diffstat (limited to 'tpblite')
-rw-r--r-- | tpblite/models/constants.py | 105 | ||||
-rw-r--r-- | tpblite/models/utils.py | 83 |
2 files changed, 115 insertions, 73 deletions
diff --git a/tpblite/models/constants.py b/tpblite/models/constants.py new file mode 100644 index 0000000..214b379 --- /dev/null +++ b/tpblite/models/constants.py @@ -0,0 +1,105 @@ + +class OPTION: + @classmethod + def printOptions(cls): + for opt in [x for x in cls.__dict__.keys() if not (x.startswith('__') or x.startswith('printOptions'))]: + if hasattr(getattr(cls,opt),'__dict__'): + for sub_opt in [y for y in getattr(cls,opt).__dict__.keys() if not (y.startswith('__') or y.startswith('printOptions'))]: + print('{}.{}'.format(opt,sub_opt)) + else: + print(opt) + +class ORDERS(OPTION): + + class NAME(OPTION): + DES = 1 + ASC = 2 + + class UPLOADED(OPTION): + DES = 3 + ASC = 4 + + class SIZE(OPTION): + DES = 5 + ASC = 6 + + class SEEDERS(OPTION): + DES = 7 + ASC = 8 + + class LEECHERS(OPTION): + DES = 9 + ASC = 10 + + class UPLOADER(OPTION): + DES = 11 + ASC = 12 + + class TYPE(OPTION): + DES = 13 + ASC = 14 + + +class CATEGORIES(OPTION): + ALL = 0 + + class AUDIO(OPTION): + ALL = 100 + MUSIC = 101 + AUDIO_BOOKS = 102 + SOUND_CLIPS = 103 + FLAC = 104 + OTHER = 199 + + class VIDEO(OPTION): + ALL = 200 + MOVIES = 201 + MOVIES_DVDR = 202 + MUSIC_VIDEOS = 203 + MOVIE_CLIPS = 204 + TV_SHOWS = 205 + HANDHELD = 206 + HD_MOVIES = 207 + HD_TV_SHOWS = 208 + THREE_DIMENSIONS = 209 + OTHER = 299 + + class APPLICATIONS(OPTION): + ALL = 300 + WINDOWS = 301 + MAC = 302 + UNIX = 303 + HANDHELD = 304 + IOS = 305 + ANDROID = 306 + OTHER = 399 + + class GAMES(OPTION): + ALL = 400 + PC = 401 + MAC = 402 + PSX = 403 + XBOX360 = 404 + WII = 405 + HANDHELD = 406 + IOS = 407 + ANDROID = 408 + OTHER = 499 + + class PORN(OPTION): + ALL = 500 + MOVIES = 501 + MOVIES_DVDR = 502 + PICTURES = 503 + GAMES = 504 + HD_MOVIES = 505 + MOVIE_CLIPS = 506 + OTHER = 599 + + class OTHER(OPTION): + EBOOKS = 601 + COMICS = 602 + PICTURES = 603 + COVERS = 604 + PHYSIBLES = 605 + OTHER = 699
\ No newline at end of file diff --git a/tpblite/models/utils.py b/tpblite/models/utils.py index 4aebc0d..72409b8 100644 --- a/tpblite/models/utils.py +++ b/tpblite/models/utils.py @@ -1,10 +1,7 @@ import random from urllib.request import Request, urlopen +from purl import URL as pURL -# Delete these when finished rewriting URL -from collections import OrderedDict -from purl import URL as PURL -# ============================== class Query(object): ''' @@ -13,87 +10,27 @@ class Query(object): ''' def __init__(self, query, base_url='https://tpb.party', page=0, order=99, category=0): self.base_url = base_url - self.base_path = '/search' - self.url = URL(base_url, self.base_path, - segments=['query', 'page', 'order', 'category'], - defaults=[query, str(page), str(order), str(category)], - ) + segments = ('search', query, str(page), str(order), str(category)) + self.url = URL(base_url, segments) self.webpage = self._sendRequest() - + def _sendRequest(self): req = Request(self.url, headers=headers()) return urlopen(req).read() - -### REWRITE THEN DELETE THESE - -def URL(base, path, segments=None, defaults=None): - """ - URL segment handler capable of getting and setting segments by name. The - URL is constructed by joining base, path and segments. - - For each segment a property capable of getting and setting that segment is - created dynamically. - """ - # Make a copy of the Segments class - url_class = type(Segments.__name__, Segments.__bases__, - dict(Segments.__dict__)) - segments = [] if segments is None else segments - defaults = [] if defaults is None else defaults - # For each segment attach a property capable of getting and setting it - for segment in segments: - setattr(url_class, segment, url_class._segment(segment)) - # Instantiate the class with the actual parameters - return url_class(base, path, segments, defaults) - - -class Segments(object): - - """ - URL segment handler, not intended for direct use. The URL is constructed by - joining base, path and segments. - """ - - def __init__(self, base, path, segments, defaults): - # Preserve the base URL - self.base = PURL(base, path=path) - # Map the segments and defaults lists to an ordered dict - self.segments = OrderedDict(zip(segments, defaults)) - - def build(self): - # Join base segments and segments - segments = self.base.path_segments() + tuple(self.segments.values()) - # Create a new URL with the segments replaced - url = self.base.path_segments(segments) - return url - - def __str__(self): - return self.build().as_string() - - def _get_segment(self, segment): - return self.segments[segment] - - def _set_segment(self, segment, value): - self.segments[segment] = value - - @classmethod - def _segment(cls, segment): - """ - Returns a property capable of setting and getting a segment. - """ - return property( - fget=lambda x: cls._get_segment(x, segment), - fset=lambda x, v: cls._set_segment(x, segment, v), - ) +def URL(base, segments): + u = pURL().from_string(base) + url = u.path_segments(segments) + return url.as_string() def headers(): - """ + ''' The Pirate Bay blocks requests (403 Forbidden) basing on User-Agent header, so it's probably better to rotate them. User-Agents taken from: https://techblog.willshouse.com/2012/01/03/most-common-user-agents/ - """ + ''' return { "User-Agent": random.choice(USER_AGENTS), "origin_req_host": "thepiratebay.se", |