Advertisement
Rawoas13

Untitled

Aug 20th, 2020
554
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.39 KB | None | 0 0
  1. import yql
  2.  
  3. class MemeNotFound(Exception):
  4.     """Raised when Meme is not found."""
  5.  
  6. class Repository(object):
  7.     def __init__(self):
  8.         self.yql = yql.Public()
  9.         self.yql_private = None
  10.  
  11. class MemeRepository(Repository):
  12.    
  13.     def _yql_query(self, query):
  14.         result = self.yql.execute(query)
  15.         if result.count == 1:
  16.             return [Meme(result.rows)]
  17.         return [Meme(row) for row in result.rows]
  18.    
  19.     def get(self, name):
  20.         query = 'SELECT * FROM meme.info WHERE name = "%s"' % name
  21.         meme = self._yql_query(query)
  22.         if meme:
  23.             return meme[0]
  24.         raise MemeNotFound("Meme %s was not found!" % name)
  25.  
  26.     def search(self, query, count):
  27.         query = 'SELECT * FROM meme.people(%d) WHERE query = "%s"' % (count, query)
  28.         memes = self._yql_query(query)
  29.         if memes:
  30.             return memes
  31.         raise MemeNotFound("No Meme found in your search")
  32.    
  33.     def following(self, guid, count):
  34.         query = 'SELECT * FROM meme.following(%d) WHERE owner_guid = "%s"' % (count, guid)
  35.         return self._yql_query(query)
  36.  
  37.     def followers(self, guid, count):
  38.         query = 'SELECT * FROM meme.followers(%d) WHERE owner_guid = "%s"' % (count, guid)
  39.         return self._yql_query(query)
  40.    
  41. class PostRepository(Repository):
  42.  
  43.     def __init__(self):
  44.         super(PostRepository, self).__init__()
  45.         self.meme_repository = MemeRepository()
  46.  
  47.     def _yql_query(self, query):
  48.         result = self.yql.execute(query)
  49.         if result.count == 1:
  50.             return [Post(result.rows)]
  51.         return [Post(row) for row in result.rows]
  52.  
  53.     def get(self, owner_guid, pubid):
  54.         query = 'SELECT * FROM meme.posts WHERE owner_guid = "%s" and pubid = "%s"' % (owner_guid, pubid)
  55.         return self._yql_query(query)
  56.  
  57.     def popular(self, locale, count):
  58.         query = 'SELECT * FROM meme.popular(%s) WHERE locale = "%s"' % (count, locale)
  59.         return self._yql_query(query)
  60.  
  61.     def search(self, query, count):
  62.         query = 'SELECT * FROM meme.search(%d) WHERE query = "%s"' % (count, query)
  63.         return self._yql_query(query)
  64.    
  65.     def get_by_meme(self, owner_guid, count):
  66.         query = 'SELECT * FROM meme.posts(%d) WHERE owner_guid = "%s"' % (count, owner_guid)
  67.         return self._yql_query(query)
  68.    
  69.     def get_most_reposted_by_meme(self, name, media, count):
  70.         search_for_media = ''
  71.         if media:
  72.             search_for_media = "type:%s" % media
  73.        
  74.         query = "from:%s sort:reposts %s" % (name, search_for_media)
  75.         return self.search(query, count)
  76.    
  77.     def activity(self, guid, pubid, count):
  78.         query = 'SELECT * FROM meme.post.info(%d) WHERE owner_guid = "%s" AND pubid = "%s"' % (count, guid, pubid)
  79.         return self._yql_query(query)
  80.  
  81. class Meme(object):
  82.     def __init__(self, data=None):
  83.         if data:
  84.             self.guid = data['guid']
  85.             self.name = data['name']
  86.             self.title = data['title']
  87.             self.description = data['description']
  88.             self.url = data['url']
  89.             self.avatar_url = data['avatar_url']
  90.             self.language = data['language']
  91.             self.follower_count = data['followers']
  92.        
  93.         self.meme_repository = MemeRepository()
  94.         self.post_repository = PostRepository()
  95.    
  96.     def following(self, count=10):
  97.         return self.meme_repository.following(self.guid, count)
  98.    
  99.     def followers(self, count=10):
  100.         return self.meme_repository.followers(self.guid, count)
  101.    
  102.     def posts(self, count=10):
  103.         return self.post_repository.get_by_meme(self.guid, count)
  104.    
  105.     def most_reposted_posts(self, media='', count=10):
  106.         return self.post_repository.get_most_reposted_by_meme(self.name, media, count)
  107.        
  108.     def __repr__(self):
  109.         return u'Meme[guid=%s, name=%s]' % (self.guid, self.name)
  110.  
  111. class Post(object):
  112.     def __init__(self, data=None):
  113.         if data:
  114.             #required data
  115.             self.guid = data['guid'] #meme id
  116.             self.pubid = data['pubid'] #post id
  117.             self.type = data['type']
  118.             self.timestamp = data['timestamp']
  119.        
  120.             #optional data
  121.             self.repost_count = data.get('repost_count') #absent only in comments
  122.             self.url = data.get('url') #absent only in comments
  123.             self.content = data.get('content') #absent only in comments and reposts
  124.             self.caption = data.get('caption')
  125.             self.comment = data.get('comment')        
  126.             self.origin_guid = data.get('origin_guid') #if empty then not a repost
  127.             self.origin_pubid = data.get('origin_pubid')
  128.             self.via_guid = data.get('via_guid')
  129.  
  130.             #filled memes - only if provided in data dict
  131.             self.meme = data.get('meme')
  132.             self.origin_meme = data.get('origin_meme')
  133.             self.via_meme = data.get('via_meme')
  134.        
  135.             if not self.origin_guid:
  136.                 self.is_original = True
  137.             else:
  138.                 self.is_original = False
  139.        
  140.         self.post_repository = PostRepository()
  141.    
  142.     def activity(self, count=10):
  143.         return self.post_repository.activity(self.guid, self.pubid, count)
  144.    
  145.     def __repr__(self):
  146.         return u'Post[guid=%s, pubid=%s, type=%s, reposts=%s]' % (self.guid, self.pubid, self.type, self.repost_count)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement