Esempio n. 1
0
def SetPageCategory(user, url, category_id, retries_left=10):
    user_key = models.UserKey(user)
    page_rating = ndb.Key(models.PageRating, url, parent=user_key).get()
    if page_rating is None:
        # It could be that the PageRating has not become available yet so we retry
        # a few times.
        if retries_left > 0:
            deferred.defer(SetPageCategory,
                           user,
                           url,
                           category_id,
                           retries_left - 1,
                           _queue='default')
        return

    category = None
    if category_id is not None:
        category = models.CategoryKey(category_id, user)

    if page_rating.category == category:
        return

    page_rating.category = category
    page_rating.put()

    deferred.defer(
        RatingAddedImpl,
        models.SerializeSource(
            models.Source(models.SOURCE_TYPE_USER, user.user_id(),
                          category_id)),
        url,
        page_rating.rating,
        _countdown=DELAY_BEFORE_UPDATING_CONNECTIONS.total_seconds())
 def GetSubscribers(self, publisher, positive=True):
     publisher_category = models.CategoryKey(publisher.category_id,
                                             publisher.source_id)
     return [
         connection_trainer.Connection(
             models.Source(models.SOURCE_TYPE_USER, c.subscriber_id,
                           c.SubscriberCategoryId()), c.weight)
         for c in models.Connection.query(
             models.Connection.publisher_id == publisher.source_id,
             models.Connection.publisher_category == publisher_category,
             models.Connection.version == self.connection_version,
             models.Connection.positive == positive)
     ]
Esempio n. 3
0
def AddRating(user, url, rating, source, category_id):
    user_id = models.UserKey(user).id()
    stats = models.AddRating(user_id, url, rating, source, category_id)

    # If the page belongs to a feed then register this feed.
    page_info = models.GetPageInfo(url)
    if page_info.feed_url:
        UpdateFeed(feeds.AddFeed(page_info.feed_url, page_info.feed_title))
        stats['own_feed'] = page_info.feed_url

    # If the page itself is a feed url then also register it.
    if page_info.is_feed:
        UpdateFeed(feeds.AddFeed(page_info.canonical_url, page_info.title))
        stats['own_feed'] = page_info.canonical_url

    RatingAdded(models.Source(models.SOURCE_TYPE_USER, user_id, category_id),
                url, rating)
    return stats
Esempio n. 4
0
def UpdateActiveConnectionStateMap(connection):
    start_datetime = StartDatetimeFromContext()
    updated = False
    if connection.active_datetime is None:
        connection.active_datetime = models.Source(
            connection.publisher_type, connection.publisher_id,
            connection.PublisherCategoryId()).GetLastRatingDatetime()
        if connection.active_datetime is None:
            return
        updated = True

    inactive_duration = start_datetime - connection.active_datetime
    active_days = [
        days for days in models.CONNECTION_ALL_ACTIVE_DAYS
        if inactive_duration < timedelta(days=days)
    ]
    if connection.active_days != active_days:
        connection.active_days = active_days
        updated = True
    if updated:
        connection.put()
 def GetRecommendations(self, url, subscriber, subscriber_rating):
     result = []
     # We do not want to trust the feed specified by the url's meta tags if the
     # user downvoted the url. That's because the specified feed url may not
     # deserve to be punished (e.g., the url is malicious and tries to lower the
     # feeds reputation).
     include_source_feed = (subscriber_rating == ratings.POSITIVE)
     for rating in models.GetUnifiedRatings(
             url, include_source_feed=include_source_feed):
         source = models.Source(rating['type'], rating['publisher_id'],
                                rating['publisher_category_id'])
         if source == subscriber:
             # Any following recommendations are the ones made after the subscriber
             # recommended it and are not interesting to the subscriber.
             break
         num_ratings_ago = 0
         if (source.source_type == models.SOURCE_TYPE_FEED
                 and source.source_id == url):
             # When the user recommends an RSS feed url then we do not want to
             # penalize it for every item that feed published from the beginning of
             # time. A freshly recommended feed should start at the same initial
             # connection weight no matter how many items the feed has published in
             # the past.
             num_ratings_ago = 0
         else:
             num_ratings_ago = source.GetNumRatingsSinceDate(rating['date'])
         # The number includes this rating so we decrease it by one.
         if num_ratings_ago > 0:
             num_ratings_ago -= 1
         result.append(
             connection_trainer.Rating(rating=rating['rating'],
                                       user=source,
                                       weight=self.GetWeight(
                                           subscriber, source,
                                           rating['rating'] > 0),
                                       num_ratings_ago=num_ratings_ago))
     return result
Esempio n. 6
0
def _DecayConnectionWeightToFeed(feed, num_new_items):
    publisher = models.Source(models.SOURCE_TYPE_FEED, feed.GetUrl(), None)
    connection_trainer.CreateTrainer().DecayConnectionWeightToPublisher(
        publisher, num_items=num_new_items)