def import_public_posts(self): """ Load the JSON of public posts for this user and create local posts from them. """ url = self.server + 'people/{0}'.format(self.guid) req = Request(url) req.add_header('User-Agent', USER_AGENT) req.add_header('Accept', 'application/json') entries = json_load(urlopen(req, timeout=10)) if isinstance(entries, dict): return # Faulty node? for entry in entries: user_guid = entry['author']['guid'] username = entry['author']['diaspora_id'] user = self if self.username == username \ else DiasporaContact.get_by_username(username, commit=False) if not user or user.guid != user_guid: continue post_guid = entry['guid'] existing_post = DiasporaPost.get_by_guid(post_guid) if existing_post or not entry['public']: continue # Already imported if entry.get('root'): root_guid = entry['root']['guid'] root_post = DiasporaPost.get_by_guid(root_guid) if root_post: parent = root_post.post else: continue # Cannot find parent else: parent = None post = Post(author=user.contact, parent=parent) db.session.add(post) post.created_at = datetime.strptime( entry['created_at'], '%Y-%m-%dT%H:%M:%SZ' ) post.thread_modified(when=datetime.strptime( entry['interacted_at'], '%Y-%m-%dT%H:%M:%SZ' )) post.add_part( MimePart( type='text/x-markdown', body=entry['text'].encode('utf-8'), text_preview=entry['text'] ), inline=True, order=0 ) post.share_with([user.contact], show_on_wall=True) post.diasp = DiasporaPost(guid=post_guid, type='public')
def import_public_posts(self): """ Load the JSON of public posts for this user and create local posts from them. """ url = self.server + 'people/{0}'.format(self.guid) req = Request(url) req.add_header('User-Agent', USER_AGENT) req.add_header('Accept', 'application/json') entries = json_load(urlopen(req, timeout=10)) if isinstance(entries, dict): return # Faulty node? for entry in entries: user_guid = entry['author']['guid'] username = entry['author']['diaspora_id'] user = self if self.username == username \ else DiasporaContact.get_by_username(username, commit=False) if not user or user.guid != user_guid: continue post_guid = entry['guid'] existing_post = DiasporaPost.get_by_guid(post_guid) if existing_post or not entry['public']: continue # Already imported if entry.get('root'): root_guid = entry['root']['guid'] root_post = DiasporaPost.get_by_guid(root_guid) if root_post: parent = root_post.post else: continue # Cannot find parent else: parent = None post = Post(author=user.contact, parent=parent) db.session.add(post) post.created_at = datetime.strptime(entry['created_at'], '%Y-%m-%dT%H:%M:%SZ') post.thread_modified(when=datetime.strptime( entry['interacted_at'], '%Y-%m-%dT%H:%M:%SZ')) post.add_part(MimePart(type='text/x-markdown', body=entry['text'].encode('utf-8'), text_preview=entry['text']), inline=True, order=0) post.share_with([user.contact], show_on_wall=True) post.diasp = DiasporaPost(guid=post_guid, type='public')