def do_send_to_twitter(post_id, app_config): with async_app_context(app_config): current_app.logger.debug('auto-posting to twitter for %s', post_id) post = Post.load_by_id(post_id) in_reply_to, repost_of, like_of = util.posse_post_discovery( post, PERMALINK_RE) # cowardly refuse to auto-POSSE a reply/repost/like when the # target tweet is not found. if post.in_reply_to and not in_reply_to: current_app.logger.warn( 'could not find tweet to reply to for %s', post.in_reply_to) return None elif post.repost_of and not repost_of: current_app.logger.warn( 'could not find tweet to repost for %s', post.repost_of) preview, img_url = guess_raw_share_tweet_content(post) elif post.like_of and not like_of: current_app.logger.warn( 'could not find tweet to like for %s', post.like_of) return None else: preview, img_url = guess_tweet_content(post, in_reply_to) response = do_tweet(post_id, preview, img_url, in_reply_to, repost_of, like_of) return str(response)
def do_send_to_twitter(post_id, app_config): with async_app_context(app_config): current_app.logger.debug('auto-posting to twitter for %s', post_id) post = Post.load_by_id(post_id) in_reply_to, repost_of, like_of = util.posse_post_discovery( post, PERMALINK_RE) # cowardly refuse to auto-POSSE a reply/repost/like when the # target tweet is not found. if post.in_reply_to and not in_reply_to: current_app.logger.warn('could not find tweet to reply to for %s', post.in_reply_to) return None elif post.repost_of and not repost_of: current_app.logger.warn('could not find tweet to repost for %s', post.repost_of) preview, img_url = guess_raw_share_tweet_content(post) elif post.like_of and not like_of: current_app.logger.warn('could not find tweet to like for %s', post.like_of) return None else: preview, img_url = guess_tweet_content(post, in_reply_to) response = do_tweet(post_id, preview, img_url, in_reply_to, repost_of, like_of) return str(response)
def do_send_to_wordpress(post_id, app_config): with async_app_context(app_config): post = Post.load_by_id(post_id) if post.like_of: for url in post.like_of: try_post_like(url, post) elif post.in_reply_to: for url in post.in_reply_to: try_post_reply(url, post)
def do_reverse_geocode_post(postid, app_config): with async_app_context(app_config): post = Post.load_by_id(postid) if post.location and 'latitude' in post.location \ and 'longitude' in post.location: adr = do_reverse_geocode(post.location['latitude'], post.location['longitude']) # copy the dict so that the ORM recognizes # that it changed post.location = dict(post.location) post.location.update(adr) db.session.commit()
def do_reverse_geocode_venue(venueid, app_config): with async_app_context(app_config): venue = Venue.query.get(venueid) if venue.location and 'latitude' in venue.location \ and 'longitude' in venue.location: adr = do_reverse_geocode(venue.location['latitude'], venue.location['longitude']) # copy the dict so the ORM actually recognizes # that it changed venue.location = dict(venue.location) venue.location.update(adr) venue.update_slug(views.geo_name(venue.location)) db.session.commit()
def do_reverse_geocode_venue(venueid, app_config): with async_app_context(app_config): venue = Venue.query.get(venueid) if venue.location and 'latitude' in venue.location \ and 'longitude' in venue.location: adr = do_reverse_geocode(venue.location['latitude'], venue.location['longitude']) # copy the dict so the ORM actually recognizes # that it changed venue.location = dict(venue.location) venue.location.update(adr) venue.update_slug(views.geo_name(venue.location, as_html=False)) db.session.commit()
def do_reverse_geocode_post(postid, app_config): with async_app_context(app_config): post = Post.load_by_id(postid) if not post: return if post.location and 'latitude' in post.location \ and 'longitude' in post.location: adr = do_reverse_geocode(post.location['latitude'], post.location['longitude']) # copy the dict so that the ORM recognizes # that it changed post.location = dict(post.location) post.location.update(adr) db.session.commit()
def do_send_to_facebook(post_id, app_config): with async_app_context(app_config): current_app.logger.debug("auto-posting to facebook for %s", post_id) post = Post.load_by_id(post_id) message, link, name, picture = guess_content(post) facebook_url = handle_new_or_edit(post, message, link, name, picture, post.post_type == "photo", album_id=None) db.session.commit() if has_request_context(): flash( 'Shared on Facebook: <a href="{}">Original</a>, ' '<a href="{}">On Facebook</a><br/>'.format(post.permalink, facebook_url) ) return redirect(post.permalink)
def do_send_to_facebook(post_id, app_config): with async_app_context(app_config): current_app.logger.debug('auto-posting to facebook for %s', post_id) post = Post.load_by_id(post_id) message, link, name, picture = guess_content(post) facebook_url = handle_new_or_edit(post, message, link, name, picture, post.post_type == 'photo', album_id=None) db.session.commit() if has_request_context(): flash('Shared on Facebook: <a href="{}">Original</a>, ' '<a href="{}">On Facebook</a><br/>' .format(post.permalink, facebook_url)) return redirect(post.permalink)
def do_process_webmention(source, target, callback, app_config): def call_callback(result): if callback: requests.post(callback, data=result) with async_app_context(app_config): try: result = interpret_mention(source, target) if result.error: current_app.logger.warn("Failed to process webmention: %s", result.error) response = { "source": source, "target": target, "response_code": 400, "status": "error", "reason": result.error, } call_callback(response) return response if result.post and result.delete: result.post.mentions = [m for m in result.post.mentions if m.url != source] elif result.post: result.post.mentions.extend(result.mentions) db.session.commit() current_app.logger.debug("saved mentions to %s", result.post.path) hooks.fire("mention-received", post=result.post) if result.post: for mres in result.mention_results: if mres.create: send_push_notification(result.post, mres.mention, app_config) response = { "source": source, "target": target, "response_code": 200, "status": "success", "reason": "Deleted" if result.delete else "Created" if any(mres.create for mres in result.mention_results) else "Updated", } call_callback(response) return response except Exception as e: current_app.logger.exception("exception while processing webmention") response = { "source": source, "target": target, "response_code": 400, "status": "error", "reason": "exception while processing webmention {}".format(e), } call_callback(response) return response
def do_process_webmention(source, target, callback, app_config): def call_callback(result): if callback: requests.post(callback, data=result) with async_app_context(app_config): try: result = interpret_mention(source, target) if result.error: current_app.logger.warn( 'Failed to process webmention: %s', result.error) response = { 'source': source, 'target': target, 'response_code': 400, 'status': 'error', 'reason': result.error } call_callback(response) return response if result.post and result.delete: result.post.mentions = [m for m in result.post.mentions if m.url != source] elif result.post: result.post.mentions.extend(result.mentions) elif result.is_person_mention: db.session.add_all(result.mentions) db.session.commit() current_app.logger.debug("saved mentions to %s", result.post.path if result.post else '/') hooks.fire('mention-received', post=result.post) for mres in result.mention_results: if mres.create: send_push_notification(result.post, result.is_person_mention, mres.mention, app_config) response = { 'source': source, 'target': target, 'response_code': 200, 'status': 'success', 'reason': 'Deleted' if result.delete else 'Created' if any(mres.create for mres in result.mention_results) else 'Updated' } call_callback(response) return response except Exception as e: current_app.logger.exception( "exception while processing webmention") response = { 'source': source, 'target': target, 'response_code': 400, 'status': 'error', 'reason': "exception while processing webmention {}".format(e) } call_callback(response) return response
def do_send_webmentions(post_id, app_config): with async_app_context(app_config): current_app.logger.debug("sending mentions for {}".format(post_id)) post = Post.load_by_id(post_id) if post: return handle_new_or_edit(post)
def do_syndicate(post_id, target_id, app_config): with async_app_context(app_config): post = Post.query.get(post_id) target = PosseTarget.query.get(target_id) current_app.logger.debug( 'posseing %s to target %s', post.path, target.uid) data = {'access_token': target.access_token} files = None if post.repost_of: data['repost-of'] = post.repost_of[0] if post.like_of: data['like-of'] = post.like_of[0] if post.in_reply_to: data['in-reply-to'] = post.in_reply_to[0] if post.post_type == 'review': item = post.item or {} data['item[name]'] = data['item'] = item.get('name') data['item[author]'] = item.get('author') data['rating'] = post.rating data['description'] = data['description[markdown]'] = data['description[value]'] = post.content data['description[html]'] = post.content_html else: data['name'] = post.title data['content'] = data['content[markdown]'] = data['content[value]'] = post.content data['content[html]'] = post.content_html data['url'] = (post.shortlink if target.style == 'microblog' else post.permalink) if post.post_type == 'photo' and post.attachments: if len(post.attachments) == 1: a = post.attachments[0] files = {'photo': (a.filename, open(a.disk_path, 'rb'), a.mimetype)} else: files = [('photo[]', (a.filename, open(a.disk_path, 'rb'), a.mimetype)) for a in post.attachments] data['location'] = post.get_location_as_geo_uri() data['place-name'] = post.venue and post.venue.name categories = [tag.name for tag in post.tags] for person in post.people: categories.append(person.url) if person.social: categories += person.social data['category[]'] = categories resp = requests.post(target.micropub_endpoint, data=util.trim_nulls(data), files=files) resp.raise_for_status() current_app.logger.debug( 'received response from posse endpoint: code=%d, headers=%s, body=%s', resp.status_code, resp.headers, resp.text) post.add_syndication_url(resp.headers['Location']) db.session.commit()
def do_process_webmention(source, target, callback, app_config): def call_callback(result): if callback: requests.post(callback, data=result) with async_app_context(app_config): try: result = interpret_mention(source, target) if result.error: current_app.logger.warn('Failed to process webmention: %s', result.error) response = { 'source': source, 'target': target, 'response_code': 400, 'status': 'error', 'reason': result.error } call_callback(response) return response if result.post and result.delete: result.post.mentions = [ m for m in result.post.mentions if m.url != source ] elif result.post: result.post.mentions.extend(result.mentions) elif result.is_person_mention: db.session.add_all(result.mentions) db.session.commit() current_app.logger.debug("saved mentions to %s", result.post.path if result.post else '/') hooks.fire('mention-received', post=result.post) for mres in result.mention_results: if mres.create: send_push_notification(result.post, result.is_person_mention, mres.mention, app_config) response = { 'source': source, 'target': target, 'response_code': 200, 'status': 'success', 'reason': 'Deleted' if result.delete else 'Created' if any( mres.create for mres in result.mention_results) else 'Updated' } call_callback(response) return response except Exception as e: current_app.logger.exception( "exception while processing webmention") response = { 'source': source, 'target': target, 'response_code': 400, 'status': 'error', 'reason': "exception while processing webmention {}".format(e) } call_callback(response) return response