def timeline(request, username, child_slug): child = get_object_or_404(Child, user__username=username, slug=child_slug) children = Child.objects.filter(user__username__exact=username) facebook_events = [] for facebook_source in child.facebook_sources.all(): posts = facebook.list_posts(facebook_source.access_token) events = keywords_present( posts, comma_split(facebook_source.keywords), facebook.post_text) facebook_events.extend(events) youtube_source = child.youtube_source youtube_events = [] if youtube_source.usernames: for youtube_user in comma_split(youtube_source.usernames): youtube_events.extend( keywords_present(youtube.list_videos(youtube_user), comma_split(youtube_source.keywords), lambda video: video.title)) events = [FacebookEvent(post) for post in facebook_events] + \ [YouTubeEvent(video) for video in youtube_events] events_json = json.dumps(events, cls=ObjectEncoder) return render(request, 'timeline/timeline.html', {'events': events_json, 'children': children, 'username': username, 'current_child_slug': child_slug})
def get_youtube_data_ajax(request, username, child_slug): keywords = comma_split(request.GET['keywords']) usernames = comma_split(request.GET['usernames']) youtube_events = [] for username in usernames: youtube_events += youtube.list_videos(username) youtube_events = keywords_present(youtube_events, keywords, lambda video: video.title) youtube_events = [YouTubeEvent(e) for e in youtube_events] return render(request, 'profiles/youtube_data.html', {'youtube_events': youtube_events})
def timeline(request, username, child_slug): child = get_object_or_404(Child, user__username=username, slug=child_slug) children = Child.objects.filter(user__username__exact=username) facebook_events = [] for facebook_source in child.facebook_sources.all(): events_key = '%s_%s' % (facebook_source.uid, make_uri_title(facebook_source.keywords)) events = cache.get(events_key) if not events: events = keywords_present(facebook.list_posts(facebook_source.access_token, first_5=True), comma_split(facebook_source.keywords), facebook.post_text) cache.set(events_key, events, 24 * 3600) facebook_events.extend(events) youtube_source = child.youtube_source youtube_events = [] if youtube_source.usernames: for youtube_user in comma_split(youtube_source.usernames): youtube_events.extend(keywords_present(youtube.list_videos(youtube_user), comma_split(youtube_source.keywords), lambda video: video.title)) events = [] events.extend([FacebookEvent(post) for post in facebook_events]) events.extend([YouTubeEvent(video) for video in youtube_events]) events_json = json.dumps(events, cls=ObjectEncoder) return render(request, "timeline/timeline.html", { 'events' : events_json, 'children' : children, 'username' : username, 'current_child_slug' : child_slug})
def get_facebook_data_ajax(request, username, child_slug): child = get_object_or_404(Child, user__username=username, slug=child_slug) keywords = comma_split(request.GET['keywords']) print keywords facebook_events = [] for source in child.facebook_sources.all(): facebook_events += facebook.list_posts(source.access_token) facebook_events = keywords_present(facebook_events, keywords, facebook.post_text) facebook_events = [FacebookEvent(e) for e in facebook_events] return render(request, 'profiles/facebook_data.html', {'facebook_events': facebook_events})
def _facebook_feed_items(access_token, keywords): graph_url = 'https://graph.facebook.com/me/feed?access_token=%s' % \ access_token keywords = comma_split(keywords) items = [] resp = json.loads(urlopen(graph_url).read()) while resp['data']: for item in resp['data']: text = item.get('message', u'') + item.get('description', u'') for keyword in keywords: if text.find(keyword) != -1: items.append(item) break resp = json.loads(urlopen(resp['paging']['next']).read()) return items
def youtube_accounts(self): results = [] for username in comma_split(self.youtube_source.usernames): results.append({'username': username, 'picture': youtube_picture(username)}) return results