def share_on_facebook(): from .twitter import collect_images if request.method == "GET": post = Post.load_by_id(request.args.get("id")) message, link, name, picture = guess_content(post) imgs = [urljoin(get_settings().site_url, img) for img in collect_images(post)] albums = [] if imgs: current_app.logger.debug("fetching user albums") resp = requests.get( "https://graph.facebook.com/v2.2/me/albums", params={"access_token": get_settings().facebook_access_token}, ) resp.raise_for_status() current_app.logger.debug("user albums response %s: %s", resp, resp.text) albums = resp.json().get("data", []) return render_template( "admin/share_on_facebook.jinja2", post=post, preview=message, link=link, name=name, picture=picture, imgs=imgs, albums=albums, ) try: post_id = request.form.get("post_id") preview = request.form.get("preview") img_url = request.form.get("img") is_photo = request.form.get("post_type") == "photo" album_id = request.form.get("album") link = request.form.get("link") if album_id == "new": album_id = create_album(request.form.get("new_album_name"), request.form.get("new_album_message")) post = Post.load_by_id(post_id) facebook_url = handle_new_or_edit( post, message=preview, link=link, name=None, picture=img_url, is_photo=is_photo, album_id=album_id ) 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) except Exception as e: if has_request_context(): current_app.logger.exception("posting to facebook") flash("Share on Facebook Failed! Exception: {}".format(e)) return redirect(url_for("views.index"))
def share_on_facebook(): from .twitter import collect_images if request.method == 'GET': post = Post.load_by_id(request.args.get('id')) message, link, name, picture = guess_content(post) imgs = [urljoin(get_settings().site_url, img) for img in collect_images(post)] albums = [] if imgs: current_app.logger.debug('fetching user albums') resp = requests.get( 'https://graph.facebook.com/v2.2/me/albums', params={'access_token': get_settings().facebook_access_token}) resp.raise_for_status() current_app.logger.debug( 'user albums response %s: %s', resp, resp.text) albums = resp.json().get('data', []) return render_template('admin/share_on_facebook.jinja2', post=post, preview=message, link=link, name=name, picture=picture, imgs=imgs, albums=albums) try: post_id = request.form.get('post_id') preview = request.form.get('preview') img_url = request.form.get('img') is_photo = request.form.get('post_type') == 'photo' album_id = request.form.get('album') link = request.form.get('link') if album_id == 'new': album_id = create_album( request.form.get('new_album_name'), request.form.get('new_album_message')) post = Post.load_by_id(post_id) facebook_url = handle_new_or_edit( post, message=preview, link=link, name=None, picture=img_url, is_photo=is_photo, album_id=album_id) 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) except Exception as e: if has_request_context(): current_app.logger.exception('posting to facebook') flash('Share on Facebook Failed! Exception: {}'.format(e)) return redirect(url_for('views.index'))
def share_on_facebook(): from .twitter import collect_images if request.method == 'GET': post = Post.load_by_id(request.args.get('id')) message, link, name, picture = guess_content(post) imgs = [urllib.parse.urljoin(get_settings().site_url, img) for img in collect_images(post)] albums = [] if imgs: current_app.logger.debug('fetching user albums') resp = requests.get( 'https://graph.facebook.com/v2.2/me/albums', params={'access_token': get_settings().facebook_access_token}) resp.raise_for_status() current_app.logger.debug( 'user albums response %s: %s', resp, resp.text) albums = resp.json().get('data', []) return render_template('admin/share_on_facebook.jinja2', post=post, preview=message, link=link, name=name, picture=picture, imgs=imgs, albums=albums) try: post_id = request.form.get('post_id') preview = request.form.get('preview') img_url = request.form.get('img') is_photo = request.form.get('post_type') == 'photo' album_id = request.form.get('album') link = request.form.get('link') if album_id == 'new': album_id = create_album( request.form.get('new_album_name'), request.form.get('new_album_message')) post = Post.load_by_id(post_id) facebook_url = handle_new_or_edit( post, message=preview, link=link, name=None, picture=img_url, is_photo=is_photo, album_id=album_id) 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) except Exception as e: if has_request_context(): current_app.logger.exception('posting to facebook') flash('Share on Facebook Failed! Exception: {}'.format(e)) return redirect(url_for('views.index'))
def edit_by_id(): id = request.args.get('id') if not id: abort(404) post = Post.load_by_id(id) if not post: abort(404) type = 'post' if not request.args.get('advanced') and post.post_type: type = post.post_type if post.draft: button_text = { 'publish': 'Publish Draft', 'publish_quietly': 'Publish Draft Quietly', 'publish+tweet': 'Publish Draft & Tweet', 'save_draft': 'Resave Draft', } else: button_text = { 'publish': 'Republish', 'publish_quietly': 'Republish Quietly', 'publish+tweet': 'Republish & Tweet', 'save_draft': 'Unpublish, Save as Draft', } template = 'admin/edit_' + type + '.jinja2' if request.args.get('full'): template = 'admin/edit_post_all.jinja2' venues = Venue.query.order_by(Venue.name).all() return render_template(template, edit_type='edit', post=post, tags=get_tags(), top_tags=get_top_tags(20), people=get_contact_nicks(), button_text=button_text, venues=venues)
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 delete_by_id(): id = request.args.get('id') post = Post.load_by_id(id) if not post: abort(404) post.deleted = True db.session.commit() hooks.fire('post-deleted', post, request.args) redirect_url = request.args.get('redirect') or url_for('views.index') current_app.logger.debug('redirecting to {}'.format(redirect_url)) return redirect(redirect_url)
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_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_tweet(post_id, preview, img_url, in_reply_to, repost_of, like_of): try: post = Post.load_by_id(post_id) twitter_url = handle_new_or_edit(post, preview, img_url, in_reply_to, repost_of, like_of) db.session.commit() if has_request_context(): flash('Shared on Twitter: <a href="{}">Original</a>, ' '<a href="{}">On Twitter</a>'.format(post.permalink, twitter_url)) return redirect(post.permalink) except Exception as e: current_app.logger.exception('posting to twitter') if has_request_context(): flash('Share on Twitter Failed!. Exception: {}'.format(e)) return redirect(url_for('views.index'))
def share_on_twitter(): if request.method == 'GET': id = request.args.get('id') if not id: abort(404) post = Post.load_by_id(id) if not post: abort(404) current_app.logger.debug('sharing on twitter. post: %s', post) in_reply_to, repost_of, like_of \ = util.posse_post_discovery(post, PERMALINK_RE) current_app.logger.debug( 'discovered in-reply-to: %s, repost-of: %s, like-of: %s', in_reply_to, repost_of, like_of) if post.repost_of and not repost_of: preview, _ = guess_raw_share_tweet_content(post) imgs = list(collect_images(post.repost_contexts[0])) else: preview, _ = guess_tweet_content(post, in_reply_to) imgs = list(collect_images(post)) current_app.logger.debug('twitter post has images: %s', imgs) return render_template('admin/share_on_twitter.jinja2', preview=preview, post=post, in_reply_to=in_reply_to, repost_of=repost_of, like_of=like_of, imgs=imgs) post_id = request.form.get('post_id') preview = request.form.get('preview') img_url = request.form.get('img') in_reply_to = request.form.get('in_reply_to') repost_of = request.form.get('repost_of') like_of = request.form.get('like_of') return do_tweet(post_id, preview, img_url, in_reply_to, repost_of, like_of)
def do_tweet(post_id, preview, img_url, in_reply_to, repost_of, like_of): try: post = Post.load_by_id(post_id) twitter_url = handle_new_or_edit( post, preview, img_url, in_reply_to, repost_of, like_of) db.session.commit() if has_request_context(): flash('Shared on Twitter: <a href="{}">Original</a>, ' '<a href="{}">On Twitter</a>' .format(post.permalink, twitter_url)) return redirect(post.permalink) except Exception as e: current_app.logger.exception('posting to twitter') if has_request_context(): flash('Share on Twitter Failed!. Exception: {}'.format(e)) return redirect(url_for('views.index'))
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 save_edit(): id = request.form.get('post_id') current_app.logger.debug('saving post %s', id) post = Post.load_by_id(id) return save_post(post)
def find_target_post(target_url): current_app.logger.debug("looking for target post at %s", target_url) # follow redirects if necessary redirect_url = urllib.request.urlopen(target_url).geturl() if redirect_url and redirect_url != target_url: current_app.logger.debug("followed redirection to %s", redirect_url) target_url = redirect_url parsed_url = urllib.parse.urlparse(target_url) if not parsed_url: current_app.logger.warn("Could not parse target_url of received webmention: %s", target_url) return None try: # FIXME this is a less-than-perfect fix for hosting from a # subdirectory. The url_map may have some clever work-around. parsed_site_root = urllib.parse.urlparse(get_settings().site_url) site_prefix = parsed_site_root.path if site_prefix.endswith("/"): site_prefix = site_prefix[:-1] if not parsed_url.path.startswith(parsed_site_root.path): raise NotFound urls = current_app.url_map.bind(get_settings().site_url) path = parsed_url.path[len(site_prefix) :] current_app.logger.debug("target path with no prefix %s", path) endpoint, args = urls.match(path) current_app.logger.debug("found match for target url %r: %r", endpoint, args) except NotFound: current_app.logger.warn("Webmention could not find target for %s", parsed_url.path) return None post = None if endpoint == "views.post_by_path": year = args.get("year") month = args.get("month") slug = args.get("slug") post = Post.load_by_path("{}/{:02d}/{}".format(year, month, slug)) elif endpoint == "views.post_by_date": post_type = args.get("post_type") year = args.get("year") month = args.get("month") day = args.get("day") index = args.get("index") post = Post.load_by_date(post_type, year, month, day, index) elif endpoint == "views.post_by_old_date": post_type = args.get("post_type") yymmdd = args.get("yymmdd") year = int("20" + yymmdd[0:2]) month = int(yymmdd[2:4]) day = int(yymmdd[4:6]) post = Post.load_by_date(post_type, year, month, day, index) elif endpoint == "views.post_by_id": dbid = args.get("dbid") post = Post.load_by_id(dbid) if not post: current_app.logger.warn("Webmention target points to unknown post: {}".format(args)), return post
def send_webmentions_manually(): id = request.args.get('id') post = Post.load_by_id(id) return jsonify({ 'mentions': handle_new_or_edit(post), })
def find_target_post(target_url): current_app.logger.debug("looking for target post at %s", target_url) # follow redirects if necessary redirect_url = urllib.request.urlopen(target_url).geturl() if redirect_url and redirect_url != target_url: current_app.logger.debug("followed redirection to %s", redirect_url) target_url = redirect_url parsed_url = urllib.parse.urlparse(target_url) if not parsed_url: current_app.logger.warn( "Could not parse target_url of received webmention: %s", target_url) return None try: # FIXME this is a less-than-perfect fix for hosting from a # subdirectory. The url_map may have some clever work-around. parsed_site_root = urllib.parse.urlparse(get_settings().site_url) site_prefix = parsed_site_root.path if site_prefix.endswith('/'): site_prefix = site_prefix[:-1] if not parsed_url.path.startswith(parsed_site_root.path): raise NotFound urls = current_app.url_map.bind(get_settings().site_url) path = parsed_url.path[len(site_prefix):] current_app.logger.debug('target path with no prefix %s', path) endpoint, args = urls.match(path) current_app.logger.debug('found match for target url %r: %r', endpoint, args) except NotFound: current_app.logger.warn('Webmention could not find target for %s', parsed_url.path) return None post = None if endpoint == 'views.post_by_path': year = args.get('year') month = args.get('month') slug = args.get('slug') post = Post.load_by_path('{}/{:02d}/{}'.format(year, month, slug)) elif endpoint == 'views.post_by_date': post_type = args.get('post_type') year = args.get('year') month = args.get('month') day = args.get('day') index = args.get('index') post = Post.load_by_date(post_type, year, month, day, index) elif endpoint == 'views.post_by_old_date': post_type = args.get('post_type') yymmdd = args.get('yymmdd') year = int('20' + yymmdd[0:2]) month = int(yymmdd[2:4]) day = int(yymmdd[4:6]) post = Post.load_by_date(post_type, year, month, day, index) elif endpoint == 'views.post_by_id': dbid = args.get('dbid') post = Post.load_by_id(dbid) if not post: current_app.logger.warn( "Webmention target points to unknown post: {}".format(args)), return post