Example #1
0
 def get_last_session(cls, uid):
   cached_user = memcache.get(uid) or {}
   last_session_json = cached_user.get('last_session')
   if not last_session_json:
     user = User.get_by_key_name(uid)
     last_session = UserSession.all().filter('user ='******'-tune_in').get()
     if last_session:
       last_session_json = last_session.toJson()
       cached_user['last_session'] = last_session_json
       memcache.set(user.id, cached_user)
   return last_session_json
Example #2
0
  def add(cls, media, user, text, acl, parent_id=None, to_user=None):
    from notification import Notification

    c = Comment(media=media, user=user, text=text)
    c.mentions = [x[0] for x in re.findall(r"@\[(\d+):([a-zA-z\s]+)\]", text)]

    if parent_id:
      c.parent_comment = Comment.get_by_id(int(parent_id))
      c.is_parent = False
    elif PRIVATE_COMMENTS:
      # ACL only needed on parent. Replies don't need ACLs.
      c.acl = acl
    c.put()

    media.comment_count += 1
    media.put()

    for m in c.mentions:
      if not to_user or (to_user and m != to_user.id):
        mentioned_user = User.get_by_key_name(m)
        if not mentioned_user or True:
          # Send them notification on FB
          fetch = urlfetch.fetch(url='https://graph.facebook.com/%s/notifications' % m,
                                 payload='access_token=%s&template=%s&href=%s' %
                                 (constants.facebook_app()['APP_ACCESS_TOKEN'],
                                 '@[' + user.id + ']' + ' mentioned you in a comment on ' + media.name + '!',
                                 media.get_path()),
                                 method=urlfetch.POST)
        else:
          n = Notification.add(mentioned_user, constants.NotificationType.MENTION, c)
          broadcast.broadcastNotification(n)

    if c.is_parent:
      from useractivity import UserActivity
      broadcast.broadcastNewActivity(UserActivity.add_comment(c.user, c))
    return c
Example #3
0
 def seen_by(self, user):
   return [User.get_by_key_name(uid).toJson() for uid in self.seen if uid in user.friends]
Example #4
0
  def fetch(self, collection=None, approve_all=False):
    all_medias = []

    if self.host == MediaHost.YOUTUBE:
      if not self.channel_id:
        yt_service = get_youtube_service()
        try:
          user_entry = yt_service.GetYouTubeUserEntry(username=self.host_id)
        except Exception as e:
          logging.error(e)
          return
        desc = user_entry.content.text
        desc = desc.decode('utf-8').replace("\n", r" ") if desc else None
        
        picture = urlfetch.fetch(user_entry.thumbnail.url)
        self.name = user_entry.title.text
        self.picture = db.Blob(picture.content)
        self.description = desc
        self.link = user_entry.link[0].href
        self.channel_id = re.search('/channel/(.*)', self.link).groups()[0]
        self.put()

      
      youtube3 = get_youtube3_service()
      
      
      publishedAfter = '1970-01-01T00:00:00Z'
      if constants.PUBLISHED_AFTER:
        publishedAfter = constants.PUBLISHED_AFTER
      elif self.last_fetch:
        publishedAfter = self.last_fetch.isoformat('T') + 'Z'
      
      ordering = ['date'] if self.last_fetch else ['date', 'rating', 'viewCount']
      
      for order in ordering:
        medias = []
        next_page_token = ''
        while next_page_token is not None:
          search_response = youtube3.search().list(
            channelId=self.channel_id,
            part='id',
            order=order,
            pageToken=next_page_token,
            publishedAfter=publishedAfter,
            maxResults=20
          ).execute()
          search_ids = ''
          for item in search_response.get('items', []):
            if item['id']['kind'] == 'youtube#video':
              search_ids += item['id']['videoId'] + ','
          videos_response = youtube3.videos().list(
            id=search_ids,
            part="id,snippet,topicDetails,contentDetails,statistics"
          ).execute()
          medias = Media.add_from_snippet(videos_response.get("items", []),
                                          collection=collection,
                                          publisher=self,
                                          enforce_category=len(collection.categories) > 0,
                                          approve=approve_all)
  
          all_medias += medias
          next_page_token = search_response.get('tokenPagination', {}).get('nextPageToken')

      self.last_fetch = datetime.datetime.now()
      self.put()
    if len(all_medias) and not approve_all:
      msg = emailer.Email(emailer.Message.FETCH, data={'count': len(medias)})
      for uid in constants.SUPER_ADMINS:
        user = User.get_by_key_name(uid)
        msg.send(user)