Example #1
0
 def process_starred_items(self, stories):
     for story in stories:
         try:
             original_feed = Feed.get_feed_from_url(story["origin"]["streamId"], create=False, fetch=False)
             if not original_feed:
                 original_feed = Feed.get_feed_from_url(story["origin"]["htmlUrl"], create=False, fetch=False)
             content = story.get("content") or story.get("summary")
             story_db = {
                 "user_id": self.user.pk,
                 "starred_date": datetime.datetime.fromtimestamp(story["updated"]),
                 "story_date": datetime.datetime.fromtimestamp(story["published"]),
                 "story_title": story.get("title", story.get("origin", {}).get("title", "[Untitled]")),
                 "story_permalink": story["alternate"][0]["href"],
                 "story_guid": story["id"],
                 "story_content": content.get("content"),
                 "story_author_name": story.get("author"),
                 "story_feed_id": original_feed and original_feed.pk,
                 "story_tags": [tag for tag in story.get("categories", []) if "user/" not in tag],
             }
             logging.user(
                 self.user,
                 "~FCStarring: ~SB%s~SN in ~SB%s" % (story_db["story_title"][:50], original_feed and original_feed),
             )
             MStarredStory.objects.create(**story_db)
         except OperationError:
             logging.user(self.user, "~FCAlready starred: ~SB%s" % (story_db["story_title"][:50]))
         except Exception, e:
             logging.user(self.user, "~FC~BRFailed to star: ~SB%s / %s" % (story, e))
Example #2
0
 def process_starred_items(self, stories):
     for story in stories:
         try:
             original_feed = Feed.get_feed_from_url(story['origin']['streamId'], create=False, fetch=False)
             if not original_feed:
                 original_feed = Feed.get_feed_from_url(story['origin']['htmlUrl'], create=False, fetch=False)
             content = story.get('content') or story.get('summary')
             story_db = {
                 "user_id": self.user.pk,
                 "starred_date": datetime.datetime.fromtimestamp(story['updated']),
                 "story_date": datetime.datetime.fromtimestamp(story['published']),
                 "story_title": story.get('title', story.get('origin', {}).get('title', '[Untitled]')),
                 "story_permalink": story['alternate'][0]['href'],
                 "story_guid": story['id'],
                 "story_content": content.get('content'),
                 "story_author_name": story.get('author'),
                 "story_feed_id": original_feed and original_feed.pk,
                 "story_tags": [tag for tag in story.get('categories', []) if 'user/' not in tag]
             }
             logging.user(self.user, "~FCStarring: ~SB%s~SN in ~SB%s" % (story_db['story_title'][:50], original_feed and original_feed))
             MStarredStory.objects.create(**story_db)
         except OperationError:
             logging.user(self.user, "~FCAlready starred: ~SB%s" % (story_db['story_title'][:50]))
         except Exception, e:
             logging.user(self.user, "~FC~BRFailed to star: ~SB%s / %s" % (story, e))
             
Example #3
0
def api_share_new_story(request):
    user = request.user
    body = request.body_json
    fields = body.get('actionFields')
    story_url = urlnorm.normalize(fields['story_url'])
    content = fields.get('story_content', "")
    story_title = fields.get('story_title', "[Untitled]")
    story_author = fields.get('story_author', "")
    comments = fields.get('comments', None)

    feed = Feed.get_feed_from_url(story_url, create=True, fetch=True)
    
    content = lxml.html.fromstring(content)
    content.make_links_absolute(story_url)
    content = lxml.html.tostring(content)
    
    shared_story = MSharedStory.objects.filter(user_id=user.pk,
                                               story_feed_id=feed and feed.pk or 0,
                                               story_guid=story_url).limit(1).first()
    if not shared_story:
        story_db = {
            "story_guid": story_url,
            "story_permalink": story_url,
            "story_title": story_title,
            "story_feed_id": feed and feed.pk or 0,
            "story_content": content,
            "story_author": story_author,
            "story_date": datetime.datetime.now(),
            "user_id": user.pk,
            "comments": comments,
            "has_comments": bool(comments),
        }
        shared_story = MSharedStory.objects.create(**story_db)
        socialsubs = MSocialSubscription.objects.filter(subscription_user_id=user.pk)
        for socialsub in socialsubs:
            socialsub.needs_unread_recalc = True
            socialsub.save()
        logging.user(request, "~BM~FYSharing story from ~SB~FCIFTTT~FY: ~SB%s: %s" % (story_url, comments))
    else:
        logging.user(request, "~BM~FY~SBAlready~SN shared story from ~SB~FCIFTTT~FY: ~SB%s: %s" % (story_url, comments))
    
    try:
        socialsub = MSocialSubscription.objects.get(user_id=user.pk, 
                                                    subscription_user_id=user.pk)
    except MSocialSubscription.DoesNotExist:
        socialsub = None
    
    if socialsub:
        socialsub.mark_story_ids_as_read([shared_story.story_hash], 
                                          shared_story.story_feed_id, 
                                          request=request)
    else:
        RUserStory.mark_read(user.pk, shared_story.story_feed_id, shared_story.story_hash)

    shared_story.publish_update_to_subscribers()
    
    return {"data": [{
        "id": shared_story and shared_story.story_guid,
        "url": shared_story and shared_story.blurblog_permalink()
    }]}
Example #4
0
def search_feed(request):
    address = request.REQUEST['address']
    offset = int(request.REQUEST.get('offset', 0))
    feed = Feed.get_feed_from_url(address, create=False, aggressive=True, offset=offset)
    
    if feed:
        return feed.canonical()
    else:
        return dict(code=-1, message="No feed found matching that XML or website address.")
Example #5
0
    def add_subscription(cls, user, feed_address, folder=None, bookmarklet=False, auto_active=True,
                         skip_fetch=False):
        feed = None
        us = None
    
        logging.user(user, "~FRAdding URL: ~SB%s (in %s) %s" % (feed_address, folder, 
                                                                "~FCAUTO-ADD" if not auto_active else ""))
    
        feed = Feed.get_feed_from_url(feed_address)

        if not feed:    
            code = -1
            if bookmarklet:
                message = "This site does not have an RSS feed. Nothing is linked to from this page."
            else:
                message = "This address does not point to an RSS feed or a website with an RSS feed."
        else:
            us, subscription_created = cls.objects.get_or_create(
                feed=feed, 
                user=user,
                defaults={
                    'needs_unread_recalc': True,
                    'active': auto_active,
                }
            )
            code = 1
            message = ""
    
        if us:
            user_sub_folders_object, created = UserSubscriptionFolders.objects.get_or_create(
                user=user,
                defaults={'folders': '[]'}
            )
            if created:
                user_sub_folders = []
            else:
                user_sub_folders = json.decode(user_sub_folders_object.folders)
            user_sub_folders = add_object_to_folder(feed.pk, folder, user_sub_folders)
            user_sub_folders_object.folders = json.encode(user_sub_folders)
            user_sub_folders_object.save()
            
            if auto_active or user.profile.is_premium:
                us.active = True
                us.save()
        
            if not skip_fetch and feed.last_update < datetime.datetime.utcnow() - datetime.timedelta(days=1):
                feed = feed.update()
            
            from apps.social.models import MActivity
            MActivity.new_feed_subscription(user_id=user.pk, feed_id=feed.pk, feed_title=feed.title)
                
            feed.setup_feed_for_premium_subscribers()
        
        return code, message, us
Example #6
0
def search_feed(request):
    address = request.REQUEST.get("address")
    offset = int(request.REQUEST.get("offset", 0))
    if not address:
        return dict(code=-1, message="Please provide a URL/address.")

    feed = Feed.get_feed_from_url(address, create=False, aggressive=True, offset=offset)
    if feed:
        return feed.canonical()
    else:
        return dict(code=-1, message="No feed found matching that XML or website address.")
Example #7
0
def search_feed(request):
    address = request.REQUEST.get('address')
    offset = int(request.REQUEST.get('offset', 0))
    if not address:
        return dict(code=-1, message="Please provide a URL/address.")
    
    logging.user(request.user, "~FBFinding feed (search_feed): %s" % address)
    ip = request.META.get('HTTP_X_FORWARDED_FOR', None) or request.META['REMOTE_ADDR']
    logging.user(request.user, "~FBIP: %s" % ip)
    aggressive = request.user.is_authenticated()
    feed = Feed.get_feed_from_url(address, create=False, aggressive=aggressive, offset=offset)
    if feed:
        return feed.canonical()
    else:
        return dict(code=-1, message="No feed found matching that XML or website address.")
Example #8
0
def api_save_new_story(request):
    user = request.user
    body = request.body_json
    fields = body.get('actionFields')
    story_url = urlnorm.normalize(fields['story_url'])
    story_content = fields.get('story_content', "")
    story_title = fields.get('story_title', "")
    story_author = fields.get('story_author', "")
    user_tags = fields.get('user_tags', "")
    story = None
    
    logging.user(request.user, "~FBFinding feed (api_save_new_story): %s" % story_url)
    original_feed = Feed.get_feed_from_url(story_url)
    if not story_content or not story_title:
        ti = TextImporter(feed=original_feed, story_url=story_url, request=request)
        original_story = ti.fetch(return_document=True)
        if original_story:
            story_url = original_story['url']
            if not story_content:
                story_content = original_story['content']
            if not story_title:
                story_title = original_story['title']
    try:
        story_db = {
            "user_id": user.pk,
            "starred_date": datetime.datetime.now(),
            "story_date": datetime.datetime.now(),
            "story_title": story_title or '[Untitled]',
            "story_permalink": story_url,
            "story_guid": story_url,
            "story_content": story_content,
            "story_author_name": story_author,
            "story_feed_id": original_feed and original_feed.pk or 0,
            "user_tags": [tag for tag in user_tags.split(',')]
        }
        story = MStarredStory.objects.create(**story_db)
        logging.user(request, "~FCStarring by ~SBIFTTT~SN: ~SB%s~SN in ~SB%s" % (story_db['story_title'][:50], original_feed and original_feed))
        MStarredStoryCounts.count_for_user(user.pk)
    except OperationError:
        logging.user(request, "~FCAlready starred by ~SBIFTTT~SN: ~SB%s" % (story_db['story_title'][:50]))
        pass
    
    return {"data": [{
        "id": story and story.id,
        "url": story and story.story_permalink
    }]}
Example #9
0
    def add_subscription(cls, user, feed_address, folder=None, bookmarklet=False):
        feed = None
        us = None
    
        logging.user(user, "~FRAdding URL: ~SB%s (in %s)" % (feed_address, folder))
    
        feed = Feed.get_feed_from_url(feed_address)

        if not feed:    
            code = -1
            if bookmarklet:
                message = "This site does not have an RSS feed. Nothing is linked to from this page."
            else:
                message = "This address does not point to an RSS feed or a website with an RSS feed."
        else:
            us, subscription_created = cls.objects.get_or_create(
                feed=feed, 
                user=user,
                defaults={
                    'needs_unread_recalc': True,
                    'active': True,
                }
            )
            code = 1
            message = ""
    
        if us:
            user_sub_folders_object, created = UserSubscriptionFolders.objects.get_or_create(
                user=user,
                defaults={'folders': '[]'}
            )
            if created:
                user_sub_folders = []
            else:
                user_sub_folders = json.decode(user_sub_folders_object.folders)
            user_sub_folders = add_object_to_folder(feed.pk, folder, user_sub_folders)
            user_sub_folders_object.folders = json.encode(user_sub_folders)
            user_sub_folders_object.save()
        
            feed.setup_feed_for_premium_subscribers()
        
            if feed.last_update < datetime.datetime.utcnow() - datetime.timedelta(days=1):
                feed.update()

        return code, message, us
Example #10
0
def api_save_new_story(request):
    user = request.user
    body = json.decode(request.body)
    fields = body.get('actionFields')
    story_url = fields['story_url']
    story_content = fields.get('story_content', "")
    story_title = fields.get('story_title', "[Untitled]")
    story_author = fields.get('story_author', "")
    user_tags = fields.get('user_tags', "")
    story = None
    
    try:
        original_feed = Feed.get_feed_from_url(story_url)
        story_db = {
            "user_id": user.pk,
            "starred_date": datetime.datetime.now(),
            "story_date": datetime.datetime.now(),
            "story_title": story_title or '[Untitled]',
            "story_permalink": story_url,
            "story_guid": story_url,
            "story_content": story_content,
            "story_author_name": story_author,
            "story_feed_id": original_feed and original_feed.pk or 0,
            "user_tags": [tag for tag in user_tags.split(',')]
        }
        story = MStarredStory.objects.create(**story_db)
        logging.user(request, "~FCStarring by ~SBIFTTT~SN: ~SB%s~SN in ~SB%s" % (story_db['story_title'][:50], original_feed and original_feed))
        MStarredStoryCounts.count_tags_for_user(user.pk)
    except OperationError:
        logging.user(request, "~FCAlready starred by ~SBIFTTT~SN: ~SB%s" % (story_db['story_title'][:50]))
        pass
    
    return {"data": [{
        "id": story and story.id,
        "url": story and story.story_permalink
    }]}
Example #11
0
def api_share_new_story(request):
    user = request.user
    body = request.body_json
    fields = body.get('actionFields')
    story_url = urlnorm.normalize(fields['story_url'])
    story_content = fields.get('story_content', "")
    story_title = fields.get('story_title', "")
    story_author = fields.get('story_author', "")
    comments = fields.get('comments', None)
        
    logging.user(request.user, "~FBFinding feed (api_share_new_story): %s" % story_url)
    original_feed = Feed.get_feed_from_url(story_url, create=True, fetch=True)
    story_hash = MStory.guid_hash_unsaved(story_url)
    if not user.profile.is_premium and MSharedStory.feed_quota(user.pk, original_feed and original_feed.pk or 0, story_hash):
        return {"errors": [{
            'message': 'Only premium users can share multiple stories per day from the same site.'
        }]}
        
    if not story_content or not story_title:
        ti = TextImporter(feed=original_feed, story_url=story_url, request=request)
        original_story = ti.fetch(return_document=True)
        if original_story:
            story_url = original_story['url']
            if not story_content:
                story_content = original_story['content']
            if not story_title:
                story_title = original_story['title']
    
    if story_content:
        story_content = lxml.html.fromstring(story_content)
        story_content.make_links_absolute(story_url)
        story_content = lxml.html.tostring(story_content)
    
    shared_story = MSharedStory.objects.filter(user_id=user.pk,
                                               story_feed_id=original_feed and original_feed.pk or 0,
                                               story_guid=story_url).limit(1).first()
    if not shared_story:
        title_max = MSharedStory._fields['story_title'].max_length
        story_db = {
            "story_guid": story_url,
            "story_permalink": story_url,
            "story_title": story_title and story_title[:title_max] or "[Untitled]",
            "story_feed_id": original_feed and original_feed.pk or 0,
            "story_content": story_content,
            "story_author": story_author,
            "story_date": datetime.datetime.now(),
            "user_id": user.pk,
            "comments": comments,
            "has_comments": bool(comments),
        }
        try:
            shared_story = MSharedStory.objects.create(**story_db)
            socialsubs = MSocialSubscription.objects.filter(subscription_user_id=user.pk)
            for socialsub in socialsubs:
                socialsub.needs_unread_recalc = True
                socialsub.save()
            logging.user(request, "~BM~FYSharing story from ~SB~FCIFTTT~FY: ~SB%s: %s" % (story_url, comments))
        except NotUniqueError:
            logging.user(request, "~BM~FY~SBAlready~SN shared story from ~SB~FCIFTTT~FY: ~SB%s: %s" % (story_url, comments))
    else:
        logging.user(request, "~BM~FY~SBAlready~SN shared story from ~SB~FCIFTTT~FY: ~SB%s: %s" % (story_url, comments))
    
    try:
        socialsub = MSocialSubscription.objects.get(user_id=user.pk, 
                                                    subscription_user_id=user.pk)
    except MSocialSubscription.DoesNotExist:
        socialsub = None
    
    if socialsub and shared_story:
        socialsub.mark_story_ids_as_read([shared_story.story_hash], 
                                          shared_story.story_feed_id, 
                                          request=request)
    elif shared_story:
        RUserStory.mark_read(user.pk, shared_story.story_feed_id, shared_story.story_hash)
    
    if shared_story:
        shared_story.publish_update_to_subscribers()
    
    return {"data": [{
        "id": shared_story and shared_story.story_guid,
        "url": shared_story and shared_story.pytuneblog_permalink()
    }]}
Example #12
0
def share_story(request, token=None):
    code = 0
    story_url = request.REQUEST['story_url']
    comments = request.REQUEST['comments']
    title = request.REQUEST['title']
    content = request.REQUEST.get('content', None)
    rss_url = request.REQUEST.get('rss_url', None)
    feed_id = request.REQUEST.get('feed_id', None) or 0
    feed = None
    message = None
    profile = None

    if request.user.is_authenticated():
        profile = request.user.profile
    else:
        try:
            profile = Profile.objects.get(secret_token=token)
        except Profile.DoesNotExist:
            code = -1
            if token:
                message = "Not authenticated, couldn't find user by token."
            else:
                message = "Not authenticated, no token supplied and not authenticated."

    if not profile:
        return HttpResponse(json.encode({
            'code': code,
            'message': message,
            'story': None,
        }),
                            mimetype='text/plain')

    if feed_id:
        feed = Feed.get_by_id(feed_id)
    else:
        if rss_url:
            logging.user(request.user,
                         "~FBFinding feed (share_story): %s" % rss_url)
            feed = Feed.get_feed_from_url(rss_url, create=True, fetch=True)
        if not feed:
            logging.user(request.user,
                         "~FBFinding feed (share_story): %s" % story_url)
            feed = Feed.get_feed_from_url(story_url, create=True, fetch=True)
        if feed:
            feed_id = feed.pk

    if content:
        content = lxml.html.fromstring(content)
        content.make_links_absolute(story_url)
        content = lxml.html.tostring(content)
    else:
        importer = TextImporter(story=None,
                                story_url=story_url,
                                request=request,
                                debug=settings.DEBUG)
        document = importer.fetch(skip_save=True, return_document=True)
        content = document['content']
        if not title:
            title = document['title']

    shared_story = MSharedStory.objects.filter(
        user_id=profile.user.pk, story_feed_id=feed_id,
        story_guid=story_url).limit(1).first()
    if not shared_story:
        story_db = {
            "story_guid": story_url,
            "story_permalink": story_url,
            "story_title": title,
            "story_feed_id": feed_id,
            "story_content": content,
            "story_date": datetime.datetime.now(),
            "user_id": profile.user.pk,
            "comments": comments,
            "has_comments": bool(comments),
        }
        shared_story = MSharedStory.objects.create(**story_db)
        socialsubs = MSocialSubscription.objects.filter(
            subscription_user_id=profile.user.pk)
        for socialsub in socialsubs:
            socialsub.needs_unread_recalc = True
            socialsub.save()
        logging.user(
            profile.user,
            "~BM~FYSharing story from site: ~SB%s: %s" % (story_url, comments))
        message = "Sharing story from site: %s: %s" % (story_url, comments)
    else:
        shared_story.story_content = content
        shared_story.story_title = title
        shared_story.comments = comments
        shared_story.story_permalink = story_url
        shared_story.story_guid = story_url
        shared_story.has_comments = bool(comments)
        shared_story.story_feed_id = feed_id
        shared_story.save()
        logging.user(
            profile.user,
            "~BM~FY~SBUpdating~SN shared story from site: ~SB%s: %s" %
            (story_url, comments))
        message = "Updating shared story from site: %s: %s" % (story_url,
                                                               comments)
    try:
        socialsub = MSocialSubscription.objects.get(
            user_id=profile.user.pk, subscription_user_id=profile.user.pk)
    except MSocialSubscription.DoesNotExist:
        socialsub = None

    if socialsub:
        socialsub.mark_story_ids_as_read([shared_story.story_hash],
                                         shared_story.story_feed_id,
                                         request=request)
    else:
        RUserStory.mark_read(profile.user.pk, shared_story.story_feed_id,
                             shared_story.story_hash)

    shared_story.publish_update_to_subscribers()

    response = HttpResponse(json.encode({
        'code': code,
        'message': message,
        'story': shared_story,
    }),
                            mimetype='text/plain')
    response['Access-Control-Allow-Origin'] = '*'
    response['Access-Control-Allow-Methods'] = 'POST'

    return response
Example #13
0
def share_story(request, token):
    code      = 0
    story_url = request.POST['story_url']
    comments  = request.POST['comments']
    title     = request.POST['title']
    content   = request.POST['content']
    rss_url   = request.POST.get('rss_url')
    feed_id   = request.POST.get('feed_id') or 0
    feed      = None
    message   = None
    
    if not story_url:
        code = -1
    else:
        try:
            profile = Profile.objects.get(secret_token=token)
        except Profile.DoesNotExist:
            code = -1
    
    if feed_id:
        feed = Feed.get_by_id(feed_id)
    else:
        if rss_url:
            feed = Feed.get_feed_from_url(rss_url, create=True, fetch=True)
        if not feed:
            feed = Feed.get_feed_from_url(story_url, create=True, fetch=True)
        if feed:
            feed_id = feed.pk
    
    shared_story = MSharedStory.objects.filter(user_id=profile.user.pk,
                                               story_feed_id=feed_id, 
                                               story_guid=story_url).limit(1).first()
    if not shared_story:
        story_db = {
            "story_guid": story_url,
            "story_permalink": story_url,
            "story_title": title,
            "story_feed_id": feed_id,
            "story_content": content,
            "story_date": datetime.datetime.now(),
            
            "user_id": profile.user.pk,
            "comments": comments,
            "has_comments": bool(comments),
        }
        shared_story = MSharedStory.objects.create(**story_db)
        socialsubs = MSocialSubscription.objects.filter(subscription_user_id=profile.user.pk)
        for socialsub in socialsubs:
            socialsub.needs_unread_recalc = True
            socialsub.save()
        logging.user(profile.user, "~BM~FYSharing story from site: ~SB%s: %s" % (story_url, comments))
    else:
        shared_story.story_content = content
        shared_story.story_title = title
        shared_story.comments = comments
        shared_story.story_permalink = story_url
        shared_story.story_guid = story_url
        shared_story.has_comments = bool(comments)
        shared_story.story_feed_id = feed_id
        shared_story.save()
        logging.user(profile.user, "~BM~FY~SBUpdating~SN shared story from site: ~SB%s: %s" % (story_url, comments))
    
    socialsub = MSocialSubscription.objects.get(user_id=profile.user.pk, 
                                                subscription_user_id=profile.user.pk)
    socialsub.mark_story_ids_as_read([shared_story.story_guid], 
                                      shared_story.story_feed_id, 
                                      request=request)

    shared_story.publish_update_to_subscribers()
    
    response = HttpResponse(json.encode({
        'code':     code,
        'message':  message,
        'story':    None,
    }), mimetype='text/plain')
    response['Access-Control-Allow-Origin'] = '*'
    response['Access-Control-Allow-Methods'] = 'POST'
    
    return response
Example #14
0
def check_share_on_site(request, token):
    code       = 0
    story_url  = request.GET['story_url']
    rss_url    = request.GET.get('rss_url')
    callback   = request.GET['callback']
    other_stories = None
    same_stories = None
    usersub    = None
    message    = None
    user       = None
    
    if not story_url:
        code = -1
    else:
        try:
            user_profile = Profile.objects.get(secret_token=token)
            user = user_profile.user
        except Profile.DoesNotExist:
            code = -1
    
    feed = Feed.get_feed_from_url(rss_url, create=False, fetch=False)
    if not feed:
        feed = Feed.get_feed_from_url(story_url, create=False, fetch=False)
    if not feed:
        parsed_url = urlparse.urlparse(story_url)
        base_url = "%s://%s%s" % (parsed_url.scheme, parsed_url.hostname, parsed_url.path)
        feed = Feed.get_feed_from_url(base_url, create=False, fetch=False)
    if not feed:
        feed = Feed.get_feed_from_url(base_url+'/', create=False, fetch=False)
    
    if feed and user:
        try:
            usersub = UserSubscription.objects.filter(user=user, feed=feed)
        except UserSubscription.DoesNotExist:
            usersub = None
    feed_id = feed and feed.pk
    your_story, same_stories, other_stories = MSharedStory.get_shared_stories_from_site(feed_id,
                                              user_id=user_profile.user.pk, story_url=story_url)
    previous_stories = MSharedStory.objects.filter(user_id=user_profile.user.pk).order_by('-shared_date').limit(3)
    previous_stories = [{
        "user_id": story.user_id,
        "story_title": story.story_title,
        "comments": story.comments,
        "shared_date": story.shared_date,
        "relative_date": relative_timesince(story.shared_date),
        "blurblog_permalink": story.blurblog_permalink(),
    } for story in previous_stories]
    
    user_ids = set([user_profile.user.pk])
    for story in same_stories:
        user_ids.add(story['user_id'])
    for story in other_stories:
        user_ids.add(story['user_id'])
    
    users = {}
    profiles = MSocialProfile.profiles(user_ids)
    for profile in profiles:
        users[profile.user_id] = {
            "username": profile.username,
            "photo_url": profile.photo_url,
        }
        
    logging.user(user_profile.user, "~BM~FCChecking share from site: ~SB%s" % (story_url),
                 request=request)
    
    response = HttpResponse(callback + '(' + json.encode({
        'code'              : code,
        'message'           : message,
        'feed'              : feed,
        'subscribed'        : bool(usersub),
        'your_story'        : your_story,
        'same_stories'      : same_stories,
        'other_stories'     : other_stories,
        'previous_stories'  : previous_stories,
        'users'             : users,
    }) + ')', mimetype='text/plain')
    response['Access-Control-Allow-Origin'] = '*'
    response['Access-Control-Allow-Methods'] = 'GET'
    
    return response
Example #15
0
def share_story(request, token):
    code = 0
    story_url = request.POST["story_url"]
    comments = request.POST["comments"]
    title = request.POST["title"]
    content = request.POST["content"]
    rss_url = request.POST.get("rss_url")
    feed_id = request.POST.get("feed_id") or 0
    feed = None
    message = None

    if not story_url:
        code = -1
    else:
        try:
            profile = Profile.objects.get(secret_token=token)
        except Profile.DoesNotExist:
            code = -1

    if feed_id:
        feed = Feed.get_by_id(feed_id)
    else:
        if rss_url:
            feed = Feed.get_feed_from_url(rss_url, create=True, fetch=True)
        if not feed:
            feed = Feed.get_feed_from_url(story_url, create=True, fetch=True)
        if feed:
            feed_id = feed.pk

    content = lxml.html.fromstring(content)
    content.make_links_absolute(story_url)
    content = lxml.html.tostring(content)

    shared_story = (
        MSharedStory.objects.filter(user_id=profile.user.pk, story_feed_id=feed_id, story_guid=story_url)
        .limit(1)
        .first()
    )
    if not shared_story:
        story_db = {
            "story_guid": story_url,
            "story_permalink": story_url,
            "story_title": title,
            "story_feed_id": feed_id,
            "story_content": content,
            "story_date": datetime.datetime.now(),
            "user_id": profile.user.pk,
            "comments": comments,
            "has_comments": bool(comments),
        }
        shared_story = MSharedStory.objects.create(**story_db)
        socialsubs = MSocialSubscription.objects.filter(subscription_user_id=profile.user.pk)
        for socialsub in socialsubs:
            socialsub.needs_unread_recalc = True
            socialsub.save()
        logging.user(profile.user, "~BM~FYSharing story from site: ~SB%s: %s" % (story_url, comments))
    else:
        shared_story.story_content = content
        shared_story.story_title = title
        shared_story.comments = comments
        shared_story.story_permalink = story_url
        shared_story.story_guid = story_url
        shared_story.has_comments = bool(comments)
        shared_story.story_feed_id = feed_id
        shared_story.save()
        logging.user(profile.user, "~BM~FY~SBUpdating~SN shared story from site: ~SB%s: %s" % (story_url, comments))

    socialsub = MSocialSubscription.objects.get(user_id=profile.user.pk, subscription_user_id=profile.user.pk)
    socialsub.mark_story_ids_as_read([shared_story.story_hash], shared_story.story_feed_id, request=request)

    shared_story.publish_update_to_subscribers()

    response = HttpResponse(json.encode({"code": code, "message": message, "story": None}), mimetype="text/plain")
    response["Access-Control-Allow-Origin"] = "*"
    response["Access-Control-Allow-Methods"] = "POST"

    return response
Example #16
0
def check_share_on_site(request, token):
    code = 0
    story_url = request.GET["story_url"]
    rss_url = request.GET.get("rss_url")
    callback = request.GET["callback"]
    other_stories = None
    same_stories = None
    usersub = None
    message = None
    user = None

    if not story_url:
        code = -1
    else:
        try:
            user_profile = Profile.objects.get(secret_token=token)
            user = user_profile.user
        except Profile.DoesNotExist:
            code = -1

    feed = Feed.get_feed_from_url(rss_url, create=False, fetch=False)
    if not feed:
        feed = Feed.get_feed_from_url(story_url, create=False, fetch=False)
    if not feed:
        parsed_url = urlparse.urlparse(story_url)
        base_url = "%s://%s%s" % (parsed_url.scheme, parsed_url.hostname, parsed_url.path)
        feed = Feed.get_feed_from_url(base_url, create=False, fetch=False)
    if not feed:
        feed = Feed.get_feed_from_url(base_url + "/", create=False, fetch=False)

    if feed and user:
        try:
            usersub = UserSubscription.objects.filter(user=user, feed=feed)
        except UserSubscription.DoesNotExist:
            usersub = None
    feed_id = feed and feed.pk
    your_story, same_stories, other_stories = MSharedStory.get_shared_stories_from_site(
        feed_id, user_id=user_profile.user.pk, story_url=story_url
    )
    previous_stories = MSharedStory.objects.filter(user_id=user_profile.user.pk).order_by("-shared_date").limit(3)
    previous_stories = [
        {
            "user_id": story.user_id,
            "story_title": story.story_title,
            "comments": story.comments,
            "shared_date": story.shared_date,
            "relative_date": relative_timesince(story.shared_date),
            "blurblog_permalink": story.blurblog_permalink(),
        }
        for story in previous_stories
    ]

    user_ids = set([user_profile.user.pk])
    for story in same_stories:
        user_ids.add(story["user_id"])
    for story in other_stories:
        user_ids.add(story["user_id"])

    users = {}
    profiles = MSocialProfile.profiles(user_ids)
    for profile in profiles:
        users[profile.user_id] = {"username": profile.username, "photo_url": profile.photo_url}

    logging.user(user_profile.user, "~BM~FCChecking share from site: ~SB%s" % (story_url), request=request)

    response = HttpResponse(
        callback
        + "("
        + json.encode(
            {
                "code": code,
                "message": message,
                "feed": feed,
                "subscribed": bool(usersub),
                "your_story": your_story,
                "same_stories": same_stories,
                "other_stories": other_stories,
                "previous_stories": previous_stories,
                "users": users,
            }
        )
        + ")",
        mimetype="text/plain",
    )
    response["Access-Control-Allow-Origin"] = "*"
    response["Access-Control-Allow-Methods"] = "GET"

    return response
Example #17
0
def check_share_on_site(request, token):
    code = 0
    story_url = request.GET['story_url']
    rss_url = request.GET.get('rss_url')
    callback = request.GET['callback']
    other_stories = None
    same_stories = None
    usersub = None
    message = None
    user = None

    if not story_url:
        code = -1
    else:
        try:
            user_profile = Profile.objects.get(secret_token=token)
            user = user_profile.user
        except Profile.DoesNotExist:
            code = -1

    logging.user(request.user,
                 "~FBFinding feed (check_share_on_site): %s" % rss_url)
    feed = Feed.get_feed_from_url(rss_url, create=False, fetch=False)
    if not feed:
        logging.user(request.user,
                     "~FBFinding feed (check_share_on_site): %s" % story_url)
        feed = Feed.get_feed_from_url(story_url, create=False, fetch=False)
    if not feed:
        parsed_url = urlparse.urlparse(story_url)
        base_url = "%s://%s%s" % (parsed_url.scheme, parsed_url.hostname,
                                  parsed_url.path)
        logging.user(request.user,
                     "~FBFinding feed (check_share_on_site): %s" % base_url)
        feed = Feed.get_feed_from_url(base_url, create=False, fetch=False)
    if not feed:
        logging.user(
            request.user,
            "~FBFinding feed (check_share_on_site): %s" % (base_url + '/'))
        feed = Feed.get_feed_from_url(base_url + '/',
                                      create=False,
                                      fetch=False)

    if feed and user:
        try:
            usersub = UserSubscription.objects.filter(user=user, feed=feed)
        except UserSubscription.DoesNotExist:
            usersub = None
    feed_id = feed and feed.pk
    your_story, same_stories, other_stories = MSharedStory.get_shared_stories_from_site(
        feed_id, user_id=user_profile.user.pk, story_url=story_url)
    previous_stories = MSharedStory.objects.filter(
        user_id=user_profile.user.pk).order_by('-shared_date').limit(3)
    previous_stories = [{
        "user_id": story.user_id,
        "story_title": story.story_title,
        "comments": story.comments,
        "shared_date": story.shared_date,
        "relative_date": relative_timesince(story.shared_date),
        "blurblog_permalink": story.blurblog_permalink(),
    } for story in previous_stories]

    user_ids = set([user_profile.user.pk])
    for story in same_stories:
        user_ids.add(story['user_id'])
    for story in other_stories:
        user_ids.add(story['user_id'])

    users = {}
    profiles = MSocialProfile.profiles(user_ids)
    for profile in profiles:
        users[profile.user_id] = {
            "username": profile.username,
            "photo_url": profile.photo_url,
        }

    logging.user(user_profile.user,
                 "~BM~FCChecking share from site: ~SB%s" % (story_url),
                 request=request)

    response = HttpResponse(callback + '(' +
                            json.encode({
                                'code': code,
                                'message': message,
                                'feed': feed,
                                'subscribed': bool(usersub),
                                'your_story': your_story,
                                'same_stories': same_stories,
                                'other_stories': other_stories,
                                'previous_stories': previous_stories,
                                'users': users,
                            }) + ')',
                            mimetype='text/plain')
    response['Access-Control-Allow-Origin'] = '*'
    response['Access-Control-Allow-Methods'] = 'GET'

    return response
Example #18
0
def api_share_new_story(request):
    user = request.user
    body = request.body_json
    fields = body.get('actionFields')
    story_url = urlnorm.normalize(fields['story_url'])
    story_content = fields.get('story_content', "")
    story_title = fields.get('story_title', "")
    story_author = fields.get('story_author', "")
    comments = fields.get('comments', None)
        
    logging.user(request.user, "~FBFinding feed (api_share_new_story): %s" % story_url)
    original_feed = Feed.get_feed_from_url(story_url, create=True, fetch=True)
    story_hash = MStory.guid_hash_unsaved(story_url)
    feed_id = (original_feed and original_feed.pk or 0)
    if not user.profile.is_premium and MSharedStory.feed_quota(user.pk, story_hash, feed_id=feed_id):
        return {"errors": [{
            'message': 'Only premium users can share multiple stories per day from the same site.'
        }]}
    
    quota = 3
    if MSharedStory.feed_quota(user.pk, story_hash, quota=quota):
        logging.user(request, "~BM~FRNOT ~FYSharing story from ~SB~FCIFTTT~FY, over quota: ~SB%s: %s" % (story_url, comments))        
        return {"errors": [{
            'message': 'You can only share %s stories per day.' % quota
        }]}
        
    if not story_content or not story_title:
        ti = TextImporter(feed=original_feed, story_url=story_url, request=request)
        original_story = ti.fetch(return_document=True)
        if original_story:
            story_url = original_story['url']
            if not story_content:
                story_content = original_story['content']
            if not story_title:
                story_title = original_story['title']
    
    if story_content:
        story_content = lxml.html.fromstring(story_content)
        story_content.make_links_absolute(story_url)
        story_content = lxml.html.tostring(story_content)
    
    shared_story = MSharedStory.objects.filter(user_id=user.pk,
                                               story_feed_id=original_feed and original_feed.pk or 0,
                                               story_guid=story_url).limit(1).first()
    if not shared_story:
        title_max = MSharedStory._fields['story_title'].max_length
        story_db = {
            "story_guid": story_url,
            "story_permalink": story_url,
            "story_title": story_title and story_title[:title_max] or "[Untitled]",
            "story_feed_id": original_feed and original_feed.pk or 0,
            "story_content": story_content,
            "story_author_name": story_author,
            "story_date": datetime.datetime.now(),
            "user_id": user.pk,
            "comments": comments,
            "has_comments": bool(comments),
        }
        try:
            shared_story = MSharedStory.objects.create(**story_db)
            socialsubs = MSocialSubscription.objects.filter(subscription_user_id=user.pk)
            for socialsub in socialsubs:
                socialsub.needs_unread_recalc = True
                socialsub.save()
            logging.user(request, "~BM~FYSharing story from ~SB~FCIFTTT~FY: ~SB%s: %s" % (story_url, comments))
        except NotUniqueError:
            logging.user(request, "~BM~FY~SBAlready~SN shared story from ~SB~FCIFTTT~FY: ~SB%s: %s" % (story_url, comments))
    else:
        logging.user(request, "~BM~FY~SBAlready~SN shared story from ~SB~FCIFTTT~FY: ~SB%s: %s" % (story_url, comments))
    
    try:
        socialsub = MSocialSubscription.objects.get(user_id=user.pk, 
                                                    subscription_user_id=user.pk)
    except MSocialSubscription.DoesNotExist:
        socialsub = None
    
    if socialsub and shared_story:
        socialsub.mark_story_ids_as_read([shared_story.story_hash], 
                                          shared_story.story_feed_id, 
                                          request=request)
    elif shared_story:
        RUserStory.mark_read(user.pk, shared_story.story_feed_id, shared_story.story_hash)
    
    if shared_story:
        shared_story.publish_update_to_subscribers()
    
    return {"data": [{
        "id": shared_story and shared_story.story_guid,
        "url": shared_story and shared_story.blurblog_permalink()
    }]}
Example #19
0
    def add_subscription(cls,
                         user,
                         feed_address,
                         folder=None,
                         bookmarklet=False,
                         auto_active=True,
                         skip_fetch=False):
        feed = None
        us = None

        logging.user(
            user, "~FRAdding URL: ~SB%s (in %s) %s" %
            (feed_address, folder, "~FCAUTO-ADD" if not auto_active else ""))

        feed = Feed.get_feed_from_url(feed_address)

        if not feed:
            code = -1
            if bookmarklet:
                message = "This site does not have an RSS feed. Nothing is linked to from this page."
            else:
                message = "This address does not point to an RSS feed or a website with an RSS feed."
        else:
            us, subscription_created = cls.objects.get_or_create(
                feed=feed,
                user=user,
                defaults={
                    'needs_unread_recalc': True,
                    'active': auto_active,
                })
            code = 1
            message = ""

        if us:
            user_sub_folders_object, created = UserSubscriptionFolders.objects.get_or_create(
                user=user, defaults={'folders': '[]'})
            if created:
                user_sub_folders = []
            else:
                user_sub_folders = json.decode(user_sub_folders_object.folders)
            user_sub_folders = add_object_to_folder(feed.pk, folder,
                                                    user_sub_folders)
            user_sub_folders_object.folders = json.encode(user_sub_folders)
            user_sub_folders_object.save()

            if auto_active or user.profile.is_premium:
                us.active = True
                us.save()

            if not skip_fetch and feed.last_update < datetime.datetime.utcnow(
            ) - datetime.timedelta(days=1):
                feed = feed.update()

            from apps.social.models import MActivity
            MActivity.new_feed_subscription(user_id=user.pk,
                                            feed_id=feed.pk,
                                            feed_title=feed.title)

            feed.setup_feed_for_premium_subscribers()

        return code, message, us
Example #20
0
def share_story(request, token=None):
    code      = 0
    story_url = request.REQUEST['story_url']
    comments  = request.REQUEST['comments']
    title     = request.REQUEST['title']
    content   = request.REQUEST.get('content', None)
    rss_url   = request.REQUEST.get('rss_url', None)
    feed_id   = request.REQUEST.get('feed_id', None) or 0
    feed      = None
    message   = None
    profile   = None
    
    if request.user.is_authenticated():
        profile = request.user.profile
    else:
        try:
            profile = Profile.objects.get(secret_token=token)
        except Profile.DoesNotExist:
            code = -1
            if token:
                message = "Not authenticated, couldn't find user by token."
            else:
                message = "Not authenticated, no token supplied and not authenticated."

    
    if not profile:
        return HttpResponse(json.encode({
            'code':     code,
            'message':  message,
            'story':    None,
        }), mimetype='text/plain')
    
    if feed_id:
        feed = Feed.get_by_id(feed_id)
    else:
        if rss_url:
            logging.user(request.user, "~FBFinding feed (share_story): %s" % rss_url)
            feed = Feed.get_feed_from_url(rss_url, create=True, fetch=True)
        if not feed:
            logging.user(request.user, "~FBFinding feed (share_story): %s" % story_url)
            feed = Feed.get_feed_from_url(story_url, create=True, fetch=True)
        if feed:
            feed_id = feed.pk
    
    if content:
        content = lxml.html.fromstring(content)
        content.make_links_absolute(story_url)
        content = lxml.html.tostring(content)
    else:
        importer = TextImporter(story=None, story_url=story_url, request=request, debug=settings.DEBUG)
        document = importer.fetch(skip_save=True, return_document=True)
        content = document['content']
        if not title:
            title = document['title']
    
    shared_story = MSharedStory.objects.filter(user_id=profile.user.pk,
                                               story_feed_id=feed_id, 
                                               story_guid=story_url).limit(1).first()
    if not shared_story:
        story_db = {
            "story_guid": story_url,
            "story_permalink": story_url,
            "story_title": title,
            "story_feed_id": feed_id,
            "story_content": content,
            "story_date": datetime.datetime.now(),
            
            "user_id": profile.user.pk,
            "comments": comments,
            "has_comments": bool(comments),
        }
        shared_story = MSharedStory.objects.create(**story_db)
        socialsubs = MSocialSubscription.objects.filter(subscription_user_id=profile.user.pk)
        for socialsub in socialsubs:
            socialsub.needs_unread_recalc = True
            socialsub.save()
        logging.user(profile.user, "~BM~FYSharing story from site: ~SB%s: %s" % (story_url, comments))
        message = "Sharing story from site: %s: %s" % (story_url, comments)
    else:
        shared_story.story_content = content
        shared_story.story_title = title
        shared_story.comments = comments
        shared_story.story_permalink = story_url
        shared_story.story_guid = story_url
        shared_story.has_comments = bool(comments)
        shared_story.story_feed_id = feed_id
        shared_story.save()
        logging.user(profile.user, "~BM~FY~SBUpdating~SN shared story from site: ~SB%s: %s" % (story_url, comments))
        message = "Updating shared story from site: %s: %s" % (story_url, comments)
    try:
        socialsub = MSocialSubscription.objects.get(user_id=profile.user.pk, 
                                                    subscription_user_id=profile.user.pk)
    except MSocialSubscription.DoesNotExist:
        socialsub = None
    
    if socialsub:
        socialsub.mark_story_ids_as_read([shared_story.story_hash], 
                                          shared_story.story_feed_id, 
                                          request=request)
    else:
        RUserStory.mark_read(profile.user.pk, shared_story.story_feed_id, shared_story.story_hash)


    shared_story.publish_update_to_subscribers()
    
    response = HttpResponse(json.encode({
        'code':     code,
        'message':  message,
        'story':    shared_story,
    }), mimetype='text/plain')
    response['Access-Control-Allow-Origin'] = '*'
    response['Access-Control-Allow-Methods'] = 'POST'
    
    return response
Example #21
0
def save_story(request, token=None):
    code      = 0
    story_url = request.POST['story_url']
    user_tags = request.POST.getlist('user_tags') or request.REQUEST.getlist('user_tags[]') or []
    add_user_tag = request.POST.get('add_user_tag', None)
    title     = request.POST['title']
    content   = request.POST.get('content', None)
    rss_url   = request.POST.get('rss_url', None)
    user_notes = request.POST.get('user_notes', None)
    feed_id   = request.POST.get('feed_id', None) or 0
    feed      = None
    message   = None
    profile   = None
    
    if request.user.is_authenticated():
        profile = request.user.profile
    else:
        try:
            profile = Profile.objects.get(secret_token=token)
        except Profile.DoesNotExist:
            code = -1
            if token:
                message = "Not authenticated, couldn't find user by token."
            else:
                message = "Not authenticated, no token supplied and not authenticated."
    
    if not profile:
        return HttpResponse(json.encode({
            'code':     code,
            'message':  message,
            'story':    None,
        }), mimetype='text/plain')
    
    if feed_id:
        feed = Feed.get_by_id(feed_id)
    else:
        if rss_url:
            logging.user(request.user, "~FBFinding feed (save_story): %s" % rss_url)
            feed = Feed.get_feed_from_url(rss_url, create=True, fetch=True)
        if not feed:
            logging.user(request.user, "~FBFinding feed (save_story): %s" % story_url)
            feed = Feed.get_feed_from_url(story_url, create=True, fetch=True)
        if feed:
            feed_id = feed.pk
    
    if content:
        content = lxml.html.fromstring(content)
        content.make_links_absolute(story_url)
        content = lxml.html.tostring(content)
    else:
        importer = TextImporter(story=None, story_url=story_url, request=request, debug=settings.DEBUG)
        document = importer.fetch(skip_save=True, return_document=True)
        content = document['content']
        if not title:
            title = document['title']
    
    if add_user_tag:
        user_tags = user_tags + [tag for tag in add_user_tag.split(',')]
        
    starred_story = MStarredStory.objects.filter(user_id=profile.user.pk,
                                                 story_feed_id=feed_id, 
                                                 story_guid=story_url).limit(1).first()
    if not starred_story:
        story_db = {
            "story_guid": story_url,
            "story_permalink": story_url,
            "story_title": title,
            "story_feed_id": feed_id,
            "story_content": content,
            "story_date": datetime.datetime.now(),
            "starred_date": datetime.datetime.now(),
            "user_id": profile.user.pk,
            "user_tags": user_tags,
            "user_notes": user_notes,
        }
        starred_story = MStarredStory.objects.create(**story_db)
        logging.user(profile.user, "~BM~FCStarring story from site: ~SB%s: %s" % (story_url, user_tags))
        message = "Saving story from site: %s: %s" % (story_url, user_tags)
    else:
        starred_story.story_content = content
        starred_story.story_title = title
        starred_story.user_tags = user_tags
        starred_story.story_permalink = story_url
        starred_story.story_guid = story_url
        starred_story.story_feed_id = feed_id
        starred_story.user_notes = user_notes
        starred_story.save()
        logging.user(profile.user, "~BM~FC~SBUpdating~SN starred story from site: ~SB%s: %s" % (story_url, user_tags))
        message = "Updating saved story from site: %s: %s" % (story_url, user_tags)

    MStarredStoryCounts.schedule_count_tags_for_user(request.user.pk)
    
    response = HttpResponse(json.encode({
        'code':     code,
        'message':  message,
        'story':    starred_story,
    }), mimetype='text/plain')
    response['Access-Control-Allow-Origin'] = '*'
    response['Access-Control-Allow-Methods'] = 'POST'
    
    return response
Example #22
0
def share_story(request, token):
    code = 0
    story_url = request.POST['story_url']
    comments = request.POST['comments']
    title = request.POST['title']
    content = request.POST['content']
    rss_url = request.POST.get('rss_url')
    feed_id = request.POST.get('feed_id') or 0
    feed = None
    message = None

    if not story_url:
        code = -1
    else:
        try:
            profile = Profile.objects.get(secret_token=token)
        except Profile.DoesNotExist:
            code = -1

    if feed_id:
        feed = Feed.get_by_id(feed_id)
    else:
        if rss_url:
            feed = Feed.get_feed_from_url(rss_url, create=True, fetch=True)
        if not feed:
            feed = Feed.get_feed_from_url(story_url, create=True, fetch=True)
        if feed:
            feed_id = feed.pk

    content = lxml.html.fromstring(content)
    content.make_links_absolute(story_url)
    content = lxml.html.tostring(content)

    shared_story = MSharedStory.objects.filter(
        user_id=profile.user.pk, story_feed_id=feed_id,
        story_guid=story_url).limit(1).first()
    if not shared_story:
        story_db = {
            "story_guid": story_url,
            "story_permalink": story_url,
            "story_title": title,
            "story_feed_id": feed_id,
            "story_content": content,
            "story_date": datetime.datetime.now(),
            "user_id": profile.user.pk,
            "comments": comments,
            "has_comments": bool(comments),
        }
        shared_story = MSharedStory.objects.create(**story_db)
        socialsubs = MSocialSubscription.objects.filter(
            subscription_user_id=profile.user.pk)
        for socialsub in socialsubs:
            socialsub.needs_unread_recalc = True
            socialsub.save()
        logging.user(
            profile.user,
            "~BM~FYSharing story from site: ~SB%s: %s" % (story_url, comments))
    else:
        shared_story.story_content = content
        shared_story.story_title = title
        shared_story.comments = comments
        shared_story.story_permalink = story_url
        shared_story.story_guid = story_url
        shared_story.has_comments = bool(comments)
        shared_story.story_feed_id = feed_id
        shared_story.save()
        logging.user(
            profile.user,
            "~BM~FY~SBUpdating~SN shared story from site: ~SB%s: %s" %
            (story_url, comments))

    try:
        socialsub = MSocialSubscription.objects.get(
            user_id=profile.user.pk, subscription_user_id=profile.user.pk)
    except MSocialSubscription.DoesNotExist:
        socialsub = None

    if socialsub:
        socialsub.mark_story_ids_as_read([shared_story.story_hash],
                                         shared_story.story_feed_id,
                                         request=request)
    else:
        RUserStory.mark_read(profile.user.pk, shared_story.story_feed_id,
                             shared_story.story_hash)

    shared_story.publish_update_to_subscribers()

    response = HttpResponse(json.encode({
        'code': code,
        'message': message,
        'story': None,
    }),
                            mimetype='text/plain')
    response['Access-Control-Allow-Origin'] = '*'
    response['Access-Control-Allow-Methods'] = 'POST'

    return response
Example #23
0
def share_story(request, token):
    code = 0
    story_url = request.POST['story_url']
    comments = request.POST['comments']
    title = request.POST['title']
    content = request.POST['content']
    rss_url = request.POST.get('rss_url')
    feed_id = request.POST.get('feed_id') or 0
    feed = None
    message = None

    if not story_url:
        code = -1
    else:
        try:
            profile = Profile.objects.get(secret_token=token)
        except Profile.DoesNotExist:
            code = -1

    if feed_id:
        feed = Feed.objects.get(pk=feed_id)
    else:
        if rss_url:
            feed = Feed.get_feed_from_url(rss_url, create=True, fetch=True)
        if not feed:
            feed = Feed.get_feed_from_url(story_url, create=True, fetch=True)
        if feed:
            feed_id = feed.pk

    parsed_url = urlparse.urlparse(story_url)
    base_url = "%s://%s%s" % (parsed_url.scheme, parsed_url.hostname,
                              parsed_url.path)
    scrubber = SelectiveScriptScrubber(base_url=base_url)
    content = scrubber.scrub(content)
    title = strip_tags(title)

    shared_story = MSharedStory.objects.filter(
        user_id=profile.user.pk, story_feed_id=feed_id,
        story_guid=story_url).limit(1).first()
    if not shared_story:
        story_db = {
            "story_guid": story_url,
            "story_permalink": story_url,
            "story_title": title,
            "story_feed_id": feed_id,
            "story_content": content,
            "story_date": datetime.datetime.now(),
            "user_id": profile.user.pk,
            "comments": comments,
            "has_comments": bool(comments),
        }
        shared_story = MSharedStory.objects.create(**story_db)
        socialsubs = MSocialSubscription.objects.filter(
            subscription_user_id=profile.user.pk)
        for socialsub in socialsubs:
            socialsub.needs_unread_recalc = True
            socialsub.save()
        logging.user(
            profile.user,
            "~BM~FYSharing story from site: ~SB%s: %s" % (story_url, comments))
    else:
        shared_story.story_content = content
        shared_story.story_title = title
        shared_story.comments = comments
        shared_story.story_permalink = story_url
        shared_story.story_guid = story_url
        shared_story.has_comments = bool(comments)
        shared_story.story_feed_id = feed_id
        shared_story.save()
        logging.user(
            profile.user,
            "~BM~FY~SBUpdating~SN shared story from site: ~SB%s: %s" %
            (story_url, comments))

    shared_story.publish_update_to_subscribers()

    response = HttpResponse(json.encode({
        'code': code,
        'message': message,
        'story': None,
    }),
                            mimetype='text/plain')
    response['Access-Control-Allow-Origin'] = '*'
    response['Access-Control-Allow-Methods'] = 'POST'

    return response
Example #24
0
def api_share_new_story(request):
    user = request.user
    body = request.body_json
    fields = body.get('actionFields')
    story_url = urlnorm.normalize(fields['story_url'])
    story_content = fields.get('story_content', "")
    story_title = fields.get('story_title', "")
    story_author = fields.get('story_author', "")
    comments = fields.get('comments', None)

    original_feed = Feed.get_feed_from_url(story_url, create=True, fetch=True)
    
    if not story_content or not story_title:
        ti = TextImporter(feed=original_feed, story_url=story_url, request=request)
        original_story = ti.fetch(return_document=True)
        story_url = original_story['url']
        if not story_content:
            story_content = original_story['content']
        if not story_title:
            story_title = original_story['title']

    story_content = lxml.html.fromstring(story_content)
    story_content.make_links_absolute(story_url)
    story_content = lxml.html.tostring(story_content)
    
    shared_story = MSharedStory.objects.filter(user_id=user.pk,
                                               story_feed_id=original_feed and original_feed.pk or 0,
                                               story_guid=story_url).limit(1).first()
    if not shared_story:
        story_db = {
            "story_guid": story_url,
            "story_permalink": story_url,
            "story_title": story_title or "[Untitled]",
            "story_feed_id": original_feed and original_feed.pk or 0,
            "story_content": story_content,
            "story_author": story_author,
            "story_date": datetime.datetime.now(),
            "user_id": user.pk,
            "comments": comments,
            "has_comments": bool(comments),
        }
        shared_story = MSharedStory.objects.create(**story_db)
        socialsubs = MSocialSubscription.objects.filter(subscription_user_id=user.pk)
        for socialsub in socialsubs:
            socialsub.needs_unread_recalc = True
            socialsub.save()
        logging.user(request, "~BM~FYSharing story from ~SB~FCIFTTT~FY: ~SB%s: %s" % (story_url, comments))
    else:
        logging.user(request, "~BM~FY~SBAlready~SN shared story from ~SB~FCIFTTT~FY: ~SB%s: %s" % (story_url, comments))
    
    try:
        socialsub = MSocialSubscription.objects.get(user_id=user.pk, 
                                                    subscription_user_id=user.pk)
    except MSocialSubscription.DoesNotExist:
        socialsub = None
    
    if socialsub:
        socialsub.mark_story_ids_as_read([shared_story.story_hash], 
                                          shared_story.story_feed_id, 
                                          request=request)
    else:
        RUserStory.mark_read(user.pk, shared_story.story_feed_id, shared_story.story_hash)

    shared_story.publish_update_to_subscribers()
    
    return {"data": [{
        "id": shared_story and shared_story.story_guid,
        "url": shared_story and shared_story.blurblog_permalink()
    }]}