Auto-sharing popular stories. Also implementing a manual cutoff, so slightly less automatically popular stories can still be manually shared.

This commit is contained in:
Samuel Clay 2012-07-31 11:41:30 -07:00
parent 7333cbd3d1
commit 54e6d97c3d
5 changed files with 30 additions and 14 deletions

View file

@ -71,10 +71,14 @@ def add_site_load_script(request, token):
add_image = image_base64('add')
try:
profile = Profile.objects.get(secret_token=token)
usf = UserSubscriptionFolders.objects.get(
user=profile.user
)
profiles = Profile.objects.filter(secret_token=token)
if profiles:
profile = profiles[0]
usf = UserSubscriptionFolders.objects.get(
user=profile.user
)
else:
code = -1
except Profile.DoesNotExist:
code = -1
except UserSubscriptionFolders.DoesNotExist:

View file

@ -286,8 +286,8 @@ class MSocialProfile(mongo.Document):
if include_following_user or common_follows_with_user:
if not include_following_user:
include_following_user = common_follows_with_user
params['followed_by_you'] = self.is_followed_by_user(include_following_user)
params['following_you'] = self.is_following_user(include_following_user)
params['followed_by_you'] = bool(self.is_followed_by_user(include_following_user))
params['following_you'] = bool(self.is_following_user(include_following_user))
return params
@ -1121,10 +1121,10 @@ class MSharedStory(mongo.Document):
story.save()
@classmethod
def collect_popular_stories(cls):
def collect_popular_stories(cls, cutoff=None):
from apps.statistics.models import MStatistics
shared_stories_count = sum(json.decode(MStatistics.get('stories_shared')))
cutoff = max(math.floor(.05 * shared_stories_count), 3)
cutoff = cutoff or max(math.floor(.05 * shared_stories_count), 3)
today = datetime.datetime.now() - datetime.timedelta(days=1)
map_f = """
@ -1160,14 +1160,13 @@ class MSharedStory(mongo.Document):
return stories, cutoff
@classmethod
def share_popular_stories(cls, verbose=True):
def share_popular_stories(cls, cutoff=None, verbose=True):
publish_new_stories = False
popular_profile = MSocialProfile.objects.get(username='popular')
popular_user = User.objects.get(pk=popular_profile.user_id)
shared_stories_today, cutoff = cls.collect_popular_stories()
shared_stories_today, cutoff = cls.collect_popular_stories(cutoff=cutoff)
for guid, story_info in shared_stories_today.items():
story = MStory.objects(story_feed_id=story_info['feed_id'],
story_guid=story_info['guid']).limit(1).first()
story, _ = MStory.find_story(story_info['feed_id'], story_info['guid'])
if not story:
logging.user(popular_user, "~FRPopular stories, story not found: %s" % story_info)
continue

View file

@ -1,5 +1,6 @@
from celery.task import Task
from apps.social.models import MSharedStory, MSocialProfile, MSocialServices
from utils import log as logging
class PostToService(Task):
@ -9,7 +10,7 @@ class PostToService(Task):
shared_story = MSharedStory.objects.get(id=shared_story_id)
shared_story.post_to_service(service)
except MSharedStory.DoesNotExist:
print "Story not found (%s). Can't post to: %s" % (shared_story_id, service)
logging.debug(" ---> Shared story not found (%s). Can't post to: %s" % (shared_story_id, service))
class EmailNewFollower(Task):
@ -41,3 +42,10 @@ class SyncFacebookFriends(Task):
social_services = MSocialServices.objects.get(user_id=user_id)
social_services.sync_facebook_friends()
class SharePopularStories(Task):
name = 'share-popular-stories'
def run(self, **kwargs):
logging.debug(" ---> Sharing popular stories...")
MSharedStory.share_popular_stories()

View file

@ -776,7 +776,7 @@ def find_friends(request):
if not profiles:
profiles = MSocialProfile.objects.filter(blurblog_title__icontains=query)[:limit]
profiles = [p.to_json(include_following_user=True) for p in profiles]
profiles = [p.to_json(include_following_user=request.user.pk) for p in profiles]
return dict(profiles=profiles)
@ajax_login_required

View file

@ -342,6 +342,11 @@ CELERYBEAT_SCHEDULE = {
'schedule': datetime.timedelta(minutes=1),
'options': {'queue': 'beat_tasks'},
},
'share-popular-stories': {
'task': 'share-popular-stories',
'schedule': datetime.timedelta(hours=1),
'options': {'queue': 'beat_tasks'},
},
}
# ====================