Permalink
Browse files

Adding unread and focus API endpoints for IFTTT. Includes both feeds …

…and folders.
  • Loading branch information...
samuelclay committed Jan 28, 2014
1 parent 1806071 commit ce434018fe95794440ec423182e9429a32536e5f
Showing with 105 additions and 1 deletion.
  1. +24 −0 apps/analyzer/models.py
  2. +1 −0 apps/oauth/urls.py
  3. +79 −0 apps/oauth/views.py
  4. +1 −1 utils/story_functions.py
View
@@ -101,6 +101,30 @@ def __unicode__(self):
feed = User.objects.get(pk=self.social_user_id)
return "%s - %s/%s: (%s) %s" % (user, self.feed_id, self.social_user_id, self.score, feed)
+
+def compute_story_score(story, classifier_titles, classifier_authors, classifier_tags, classifier_feeds):
+ intelligence = {
+ 'feed': apply_classifier_feeds(classifier_feeds, story['story_feed_id']),
+ 'author': apply_classifier_authors(classifier_authors, story),
+ 'tags': apply_classifier_tags(classifier_tags, story),
+ 'title': apply_classifier_titles(classifier_titles, story),
+ }
+ score = 0
+ score_max = max(intelligence['title'],
+ intelligence['author'],
+ intelligence['tags'])
+ score_min = min(intelligence['title'],
+ intelligence['author'],
+ intelligence['tags'])
+ if score_max > 0:
+ score = score_max
+ elif score_min < 0:
+ score = score_min
+
+ if score == 0:
+ score = intelligence['feed']
+
+ return score
def apply_classifier_titles(classifiers, story):
score = 0
View
@@ -17,4 +17,5 @@
url(r'^oauth2/token/?$', op_views.TokenView.as_view(), name="ifttt-token"),
url(r'^user/info/?$', views.api_user_info, name="ifttt-user-info"),
url(r'^triggers/(?P<trigger_slug>\w+)/fields/feed_or_folder/options/?$', views.api_feed_list, name="ifttt-trigger-feedlist"),
+ url(r'^triggers/(?P<unread_score>(new-unread-story|new-focus-story))/?$', views.api_unread_story, name="ifttt-trigger-unreadstory"),
)
View
@@ -9,6 +9,9 @@
from apps.social.models import MSocialServices
from apps.social.tasks import SyncTwitterFriends, SyncFacebookFriends, SyncAppdotnetFriends
from apps.reader.models import UserSubscription, UserSubscriptionFolders
+from apps.analyzer.models import MClassifierTitle, MClassifierAuthor, MClassifierFeed, MClassifierTag
+from apps.analyzer.models import compute_story_score
+from apps.rss_feeds.models import Feed, MStory
from utils import log as logging
from utils.user_functions import ajax_login_required
from utils.view_functions import render_to
@@ -298,3 +301,79 @@ def api_feed_list(request, trigger_slug=None):
titles.extend(folder_contents)
return {"data": titles}
+
+@login_required
+@json.json_view
+def api_unread_story(request, unread_score=None):
+ user = request.user
+ body = json.decode(request.body)
+ after = body.get('after', None)
+ before = body.get('before', None)
+ limit = body.get('limit', 50)
+ fields = body.get('triggerFields')
+ feed_or_folder = fields['feed_or_folder']
+ entries = []
+
+ if isinstance(feed_or_folder, int):
+ feed_id = feed_or_folder
+ usersub = UserSubscription.objects.get(user=user, feed_id=feed_id)
+ found_feed_ids = [feed_id]
+ found_trained_feed_ids = [feed_id] if usersub.is_trained else []
+ stories = usersub.get_stories(order="newest", read_filter="unread",
+ offset=0, limit=limit,
+ default_cutoff_date=user.profile.unread_cutoff)
+ else:
+ folder_title = feed_or_folder
+ usf = UserSubscriptionFolders.objects.get(user=user)
+ flat_folders = usf.flatten_folders()
+ feed_ids = flat_folders.get(folder_title)
+ print flat_folders, folder_title, feed_ids
+ usersubs = UserSubscription.subs_for_feeds(user.pk, feed_ids=feed_ids,
+ read_filter="unread")
+ feed_ids = [sub.feed_id for sub in usersubs]
+ print unread_score, feed_ids, folder_title
+ params = {
+ "user_id": user.pk,
+ "feed_ids": feed_ids,
+ "offset": 0,
+ "limit": limit,
+ "order": "newest",
+ "read_filter": "unread",
+ "usersubs": usersubs,
+ "cutoff_date": user.profile.unread_cutoff,
+ }
+ story_hashes, unread_feed_story_hashes = UserSubscription.feed_stories(**params)
+ mstories = MStory.objects(story_hash__in=story_hashes).order_by('-story_date')
+ stories = Feed.format_stories(mstories)
+ found_feed_ids = list(set([story['story_feed_id'] for story in stories]))
+ trained_feed_ids = [sub.feed_id for sub in usersubs if sub.is_trained]
+ found_trained_feed_ids = list(set(trained_feed_ids) & set(found_feed_ids))
+
+ if found_trained_feed_ids:
+ classifier_feeds = list(MClassifierFeed.objects(user_id=user.pk,
+ feed_id__in=found_trained_feed_ids))
+ classifier_authors = list(MClassifierAuthor.objects(user_id=user.pk,
+ feed_id__in=found_trained_feed_ids))
+ classifier_titles = list(MClassifierTitle.objects(user_id=user.pk,
+ feed_id__in=found_trained_feed_ids))
+ classifier_tags = list(MClassifierTag.objects(user_id=user.pk,
+ feed_id__in=found_trained_feed_ids))
+
+ for story in stories:
+ score = 0
+ if found_trained_feed_ids and story['story_feed_id'] in found_trained_feed_ids:
+ score = compute_story_score(story, classifier_titles=classifier_titles,
+ classifier_authors=classifier_authors,
+ classifier_tags=classifier_tags,
+ classifier_feeds=classifier_feeds)
+ if score < 0: continue
+ if unread_score == "new-focus-story" and score < 1: continue
+ entries.append({
+ "story_title": story['story_title'],
+ "story_content": story['story_content'],
+ "story_url": story['story_permalink'],
+ "story_date": story['story_date'],
+ "story_score": score,
+ })
+
+ return {"data": entries}
View
@@ -330,4 +330,4 @@ def htmldiff(old_html, new_html):
result = htmldiff_tokens(old_html_tokens, new_html_tokens)
result = ''.join(result).strip()
- return fixup_ins_del_tags(result)
+ return fixup_ins_del_tags(result)

0 comments on commit ce43401

Please sign in to comment.