Commits

Paul Tan  committed d003dce

Replaced IllustModeParseCache with req_illust.
req_illust uses functools.lru_cache to provide the caching
instead of the custom implementation in IllustModeParseCache.

  • Participants
  • Parent commits 954f7c3

Comments (0)

Files changed (1)

 """
 from collections import namedtuple
 from abc import ABCMeta
-from functools import total_ordering
+from functools import total_ordering, lru_cache
 #TODO: Write tests
 
 __all__ = ["Illust", "Manga", "Pixiv", "Sizes"]
 Internal Data Structures for High Level API
 """
 
-class IllustModeParseCache:
-    from functools import lru_cache
-    def __init__(self, session, id):
-        self.id = int(id)
-        self.session = session
-    @lru_cache(maxsize = 20)
-    def __getitem__(self, key):
-        import requests
-        #If key is str, treat as mode. Else treat as (mode, page)
-        if isinstance(key, str):
-            mode = key
-            page = None
-        else:
-            mode = key[0]
-            page = key[1]
-        req = gen_illust_request(self.session, self.id, mode, page)
-        args = req._asdict()
-        args['allow_redirects'] = False
-        r = requests.request(**args)
-        r.raise_for_status()
-        return parse_illust_content(r.content, mode)
-
-
+#TODO: enum is a bit too fat and ugly. Rewrite.
 def enum(classname, names):
     class EnumClass:
         __slots__ = names
             break
     f.close()
     return filename
+
+
+@lru_cache(maxsize = 100)
+def req_illust(session, id, mode, page = None):
+    import requests
+    req = gen_illust_request(session, id, mode, page)
+    args = req._asdict()
+    args['allow_redirects'] = False
+    r = requests.request(**args)
+    r.raise_for_status()
+    return parse_illust_content(r.content, mode)
+
     
 
 """
 class Illust:
     """Illust"""
     class Size:
-        def __init__(self, mode, parsecache):
+        def __init__(self, parent, mode):
+            self.parent = parent
             self.mode = mode
-            self.pc = parsecache
         @property
         def imgrequests(self):
-            return [gen_illust_img_request(self.pc.session, self.imgurl, self.pc.id,  self.mode)]
+            return [gen_illust_img_request(self.parent.context.session, self.imgurl, self.parent.id,  self.mode)]
+
         @property
         def imgurl(self):
-            return self.pc[self.mode].imgurl
+            session = self.parent.context.session
+            id = self.parent.id
+            return req_illust(session, id, self.mode).imgurl
+
         @property
         def imgurls(self):
             return [self.imgurl]
     def __init__(self, context, id:int):
         self.context = context
         self.id = id
-        self.pc = IllustModeParseCache(self.context.session, self.id)
-        self.sizes = dict((x, Illust.Size(x, self.pc)) for x in ('medium', 'big'))
+        self.sizes = dict((x, Illust.Size(self, x)) for x in ('medium', 'big'))
 
     @staticmethod
     def from_url(context, url):
     #NOTE: Even though internally (in Pixiv) illustrations and manga
     #are the same, they are treated differently in the high-level
     #API
+    __all__ = ['__init__', 'pages', 'from_url']
     class MediumSize:
-        def __init__(self, parsecache):
-            self.pc = parsecache
+        __all__ = ['imgurls', 'imgrequests']
+        def __init__(self, parent):
+            self.parent = parent
+
         @property
         def imgurls(self):
             """Returns the medium imgurl for each page"""
-            p = self.pc['manga']
+            p = req_illust(self.parent.context.session,
+                    self.parent.id,
+                    'manga')
             return [x.imgurl for x in p.pages]
+
         @property
         def imgrequests(self):
-            return [gen_illust_img_request(self.pc, x, self.pc.id, 'manga') for x in \
+            return [gen_illust_img_request(self.parent.context.session, x, self.parent.id, 'manga') for x in \
                     self.imgurls]
 
     class LargeSize:
-        def __init__(self, parsecache):
-            self.pc = parsecache
+        __all__ = ['imgurls', 'imgrequests']
+        def __init__(self, parent):
+            self.parent = parent
+
         @property
         def imgurls(self):
             """Returns the big imgurl for each page"""
-            p = self.pc['manga']
-            return [self.pc['manga_big', i].imgurl for i, _ in enumerate(p.pages)]
+            session = self.parent.context.session
+            p = req_illust(session, self.parent.id, 'manga')
+            return [req_illust(session, self.parent.id, 'manga_big', i).imgurl \
+                    for i, _ in enumerate(p.pages)]
+
         @property
         def imgrequests(self):
-            return [gen_illust_img_request(self.pc.session, x,  self.pc.id, 'manga_big') for x in \
+            session = self.parent.context.session
+            return [gen_illust_img_request(session, x, self.parent.id, 'manga_big') for x in \
                     self.imgurls]
 
     def __init__(self, context, id):
         self.id = id
         #Cached mode=manga request
         self._manga_content = None
-        self.pc = IllustModeParseCache(self.context.session, self.id)
-        self.sizes = {Sizes.medium: Manga.MediumSize(self.pc),
-                Sizes.large: Manga.LargeSize(self.pc)}
+        self.sizes = {Sizes.medium: Manga.MediumSize(self),
+                Sizes.large: Manga.LargeSize(self)}
 
     @property
     def pages(self):
-        return self.pc['manga'].pages
+        return req_illust(self.context.session, self.id, 'manga').pages
 
     @staticmethod
     def from_url(context, url):