def handle_get_comments_resp(self, resp, post_id, callback): from facebookapi import simplify_multiquery resp = simplify_multiquery(resp, {'comments':None, 'count':None, 'profiles':'id'} ) resp['comments'].sort(key = lambda c: c['time']) try: count = resp['count'][0]['comments']['count'] try: self.last_stream['post_ids'][post_id]['comments']['count'] = int(count) except Exception: traceback.print_exc() except Exception: num_comments = len(resp['comments']) if num_comments >= 50: count = -1 else: count = num_comments self.last_stream['comments'][post_id] = resp['comments'] self.last_stream['profiles'].update(resp['profiles']) callback.success(post_id, count)
def handle_get_comments_resp(self, resp, post_id, callback): from facebookapi import simplify_multiquery resp = simplify_multiquery(resp, { 'comments': None, 'count': None, 'profiles': 'id' }) resp['comments'].sort(key=lambda c: c['time']) try: count = resp['count'][0]['comments']['count'] try: self.last_stream['post_ids'][post_id]['comments'][ 'count'] = int(count) except Exception: traceback.print_exc() except Exception: num_comments = len(resp['comments']) if num_comments >= 50: count = -1 else: count = num_comments self.last_stream['comments'][post_id] = resp['comments'] self.last_stream['profiles'].update(resp['profiles']) callback.success(post_id, count)
def get_stream_success(self, value, update=False, num_tries=0): from util import default_timer self.end_get_stream = default_timer() log.debug('stream get took %f seconds', self.end_get_stream - self.start_get_stream) stream = value # v = [] # for val in value: # v.append(simplejson.loads(val, object_hook=facebookapi.storageify)) # alerts, stream = v[:2] self.last_alerts = Alerts(self.acct) from facebookapi import simplify_multiquery try: # print stream new_stream = simplify_multiquery( stream, keys={ 'posts': None, # 'comments':None, 'latest_posts': None, 'profiles': 'id', # 'now':None, 'events': list, 'status': None, 'notifications': None, 'apps': 'app_id', 'post_filter_keys': None, 'filter_keys': 'filter_key' }) # 'birthdays':'uid',}) import util.primitives.funcs as funcs # new_stream['comments'] = dict(funcs.groupby(new_stream['comments'], lambda x: x['post_id'])) new_stream['comments'] = {} new_stream['post_ids'] = post_ids = {} for k, v in new_stream['filter_keys'].iteritems(): if not v.get('name'): v['name'] = KNOWN_APPS_LOOKUP.get(k, v.get('name')) new_stream['filter_keys'].update([ (k, dict(name=d['name'], icon_url=skin.get(d['icon_url']).path.url())) for k, d in FORCED_KEYS.items() ]) new_stream['post_filter_keys'] = dict( (post_id, structures.oset(p['filter_key'] for p in vals)) for post_id, vals in funcs.groupby( new_stream['post_filter_keys'], lambda x: x['post_id'])) for post in new_stream['posts']: post['comments']['count'] = int(post['comments']['count']) new_stream['apps'], apps_str = {}, new_stream['apps'] for app_id, app_dict in apps_str.items(): new_stream['apps'][int(app_id)] = app_dict try: new_stream['now'] = new_stream['now'][0].values()[0] except (IndexError, KeyError) as _e: # print_exc() import time new_stream['now'] = time.time() self.last_alerts.event_invites &= set(new_stream['events']) self.last_status = (new_stream['status'][:1] or [ Ostorage([('message', ''), ('status_id', 0), ('time', 0)]) ])[0] self.last_status['uid'] = self.digsby.uid if not isinstance(new_stream['posts'], list): log.error('stream: %r', stream) raise ValueError('Facebook returned type=%r of posts' % type(new_stream['posts'])) for post in new_stream['posts']: #get the new ones post_ids[post['post_id']] = post if 'notifications' in new_stream: import lxml for notification in new_stream['notifications']: title_html = notification.get('title_html', None) if title_html is None: continue s = lxml.html.fromstring(title_html) s.make_links_absolute('http://www.facebook.com', resolve_base_href=False) for a in s.findall('a'): a.tag = 'span' # _c = a.attrib.clear() a.attrib['class'] = 'link notification_link' [ x.attrib.pop("data-hovercard", None) for x in s.findall(".//*[@data-hovercard]") ] notification['title_html'] = lxml.etree.tostring(s) self.last_alerts.update_notifications( new_stream['notifications']) if update: latest_posts = filter(None, (post_ids.get( post_id, self.last_stream.post_ids.get(post_id) ) for post_id in structures.oset( [post['post_id'] for post in new_stream['latest_posts']] + [post['post_id'] for post in self.last_stream.posts])))[:POSTS_LIMIT] new_stream['posts'] = latest_posts for post in new_stream[ 'posts']: #update the dict with the combined list post_ids[post['post_id']] = post for key in self.last_stream.comments: if key in post_ids and key not in new_stream.comments: new_stream.comments[key] = self.last_stream.comments[ key] for key in self.last_stream.profiles: if key not in new_stream.profiles: new_stream.profiles[key] = self.last_stream.profiles[ key] trim_profiles(new_stream) for p in new_stream.posts: p.id = p.post_id # compatability hack for ads self.last_stream = new_stream self.social_feed.new_ids( [p['post_id'] for p in self.last_stream.posts]) except Exception, e: traceback.print_exc() return self.get_stream_error(num_tries=num_tries, error=e)
def get_stream_success(self, value, update=False, num_tries=0): from util import default_timer self.end_get_stream = default_timer() log.debug('stream get took %f seconds', self.end_get_stream - self.start_get_stream) stream = value # v = [] # for val in value: # v.append(simplejson.loads(val, object_hook=facebookapi.storageify)) # alerts, stream = v[:2] self.last_alerts = Alerts(self.acct) from facebookapi import simplify_multiquery try: # print stream new_stream = simplify_multiquery(stream,keys={'posts':None, # 'comments':None, 'latest_posts':None, 'profiles':'id', # 'now':None, 'events':list, 'status':None, 'notifications': None, 'apps' : 'app_id', 'post_filter_keys':None, 'filter_keys':'filter_key'})# 'birthdays':'uid',}) import util.primitives.funcs as funcs # new_stream['comments'] = dict(funcs.groupby(new_stream['comments'], lambda x: x['post_id'])) new_stream['comments'] = {} new_stream['post_ids'] = post_ids = {} for k, v in new_stream['filter_keys'].iteritems(): if not v.get('name'): v['name'] = KNOWN_APPS_LOOKUP.get(k, v.get('name')) new_stream['filter_keys'].update([(k, dict(name=d['name'], icon_url=skin.get(d['icon_url']).path.url())) for k,d in FORCED_KEYS.items()]) new_stream['post_filter_keys'] = dict((post_id, structures.oset(p['filter_key'] for p in vals)) for post_id, vals in funcs.groupby(new_stream['post_filter_keys'], lambda x: x['post_id'])) for post in new_stream['posts']: post['comments']['count'] = int(post['comments']['count']) new_stream['apps'], apps_str = {}, new_stream['apps'] for app_id, app_dict in apps_str.items(): new_stream['apps'][int(app_id)] = app_dict try: new_stream['now'] = new_stream['now'][0].values()[0] except (IndexError, KeyError) as _e: # print_exc() import time new_stream['now'] = time.time() self.last_alerts.event_invites &= set(new_stream['events']) self.last_status = (new_stream['status'][:1] or [Ostorage([('message', ''), ('status_id', 0), ('time', 0)])])[0] self.last_status['uid'] = self.digsby.uid if not isinstance(new_stream['posts'], list): log.error('stream: %r', stream) raise ValueError('Facebook returned type=%r of posts' % type(new_stream['posts'])) for post in new_stream['posts']: #get the new ones post_ids[post['post_id']] = post if 'notifications' in new_stream: import lxml for notification in new_stream['notifications']: title_html = notification.get('title_html', None) if title_html is None: continue s = lxml.html.fromstring(title_html) s.make_links_absolute('http://www.facebook.com', resolve_base_href = False) for a in s.findall('a'): a.tag = 'span' # _c = a.attrib.clear() a.attrib['class'] = 'link notification_link' [x.attrib.pop("data-hovercard", None) for x in s.findall(".//*[@data-hovercard]")] notification['title_html'] = lxml.etree.tostring(s) self.last_alerts.update_notifications(new_stream['notifications']) if update: latest_posts = filter(None, (post_ids.get(post_id, self.last_stream.post_ids.get(post_id)) for post_id in structures.oset([post['post_id'] for post in new_stream['latest_posts']] + [post['post_id'] for post in self.last_stream.posts])))[:POSTS_LIMIT] new_stream['posts'] = latest_posts for post in new_stream['posts']: #update the dict with the combined list post_ids[post['post_id']] = post for key in self.last_stream.comments: if key in post_ids and key not in new_stream.comments: new_stream.comments[key] = self.last_stream.comments[key] for key in self.last_stream.profiles: if key not in new_stream.profiles: new_stream.profiles[key] = self.last_stream.profiles[key] trim_profiles(new_stream) for p in new_stream.posts: p.id = p.post_id # compatability hack for ads self.last_stream = new_stream self.social_feed.new_ids([p['post_id'] for p in self.last_stream.posts]) except Exception, e: traceback.print_exc() return self.get_stream_error(num_tries=num_tries, error=e)