def fill_metainfo(self, label): self.set_device() if label: q = gdata.blogger.client.Query(categories=[label.encode('utf-8')], max_results=0) else: q = gdata.blogger.client.Query(max_results=0) feed = util.get_posts(q) if feed: self.view.blog_title = feed.title.text self.view.title = 'Blog Information' author = feed.author[0].name total_posts = int(feed.total_results.text) blog_id = util.get_settings().get('blog_id') content = [] content.append(info.Item('Author', author)) content.append(info.Item('Blog ID', blog_id)) content.append(info.Item('Blog Title', feed.title.text)) if label: self.view.title += (": search result for [%s]" % label) self.view.label = label content.append(info.Item('Total search results', total_posts)) else: content.append(info.Item('Total posts', total_posts)) self.view.content = content else: logging.error("Fail to get Atom feed")
def fill_view_attrs(self, label, page): self.set_device() start_index = (page - 1) * info.Pager.PAGESIZE + 1 if label: q = gdata.blogger.client.Query(categories=[label.encode('utf-8')], start_index=start_index, max_results=info.Pager.PAGESIZE) else: q = gdata.blogger.client.Query(start_index=start_index, max_results=info.Pager.PAGESIZE) feed = util.get_posts(q) if feed: self.view.blog_title = feed.title.text if label: self.view.title = label self.view.label_exists = True self.view.label = label else: self.view.title = feed.title.text # 'feed' is an instance of gdata.blogger.BlogPostFeed. # So, 'feed.entry' is actually a list of gdata.blogger.BlogPost. for entry in feed.entry: util.register_pair_of_links(entry) self.view.content = feed.entry total_posts = int(feed.total_results.text) self.view.pager = util.get_favorite_pager(page, total_posts) self.view.start_index = start_index # collect labels for each post, then save them. labels = util.collect_labels(feed.entry) util.save_labels(labels, self.request, self.response) else: logging.error("Fail to get Atom feed with the label %s in the page #%d" % (label, page))
def get_tag(blog, tag, fetch_images=True, subfolder=""): response = get(blog, 'posts', {'tag': tag, 'limit': 0}) total = response['total_posts'] offsets = range(0, total, 20) for offset in offsets: posts = get_posts(blog, {'tag': tag, 'offset': offset}) for post in posts: save_post(post, fetch_images, subfolder)
def get_post(blog, post_id, fetch_images=True, subfolder=''): posts = util.get_posts(blog, {'id': post_id}) assert len(posts) == 1, 'Expected only one post, received ' + str( len(posts)) post = posts[0] save_post(post, fetch_images, subfolder=subfolder)
def convert(): if util.validate_request_subreddit(request.form) is not None: return jsonify(type='danger', text=util.validate_request_subreddit(request.form)) subreddit = request.form['subreddit'] include_comments = request.form['comments'] time = request.form['time'] limit = int(request.form['limit']) address = request.form['email'] kindle_address = request.form['kindle_address'] try: posts = util.get_posts(subreddit, time, limit) if time == 'all': title = 'Top ' + str( limit) + ' posts from /r/' + subreddit + ' ever' else: title = 'Top ' + str( limit ) + ' posts from /r/' + subreddit + ' over the past ' + time top = [] for post in posts: author = '[deleted]' if post.author is None else post.author.name comments = None if include_comments == 'true': post.comments.replace_more(limit=0) comments = util.get_comments(post, request.form['comments_style'], author) try: top.append({ 'title': post.title, 'body': util.get_content(post.url) if not post.url.startswith('https://www.reddit.com/r/') else util.markdown(post.selftext), 'author': author, 'comments': comments }) except: pass except: return jsonify(type='danger', text='That ain\'t no subreddit I\'ve ever heard of!') attachment = render_template('posts.html', posts=top, title=title) status = util.send_email(address, kindle_address, attachment, title) if status is None: return jsonify(type='success', text='Success!') else: return jsonify(type='warning', text='Uh oh! Something went wrong on our end')
def convert(): if util.validate_request_subreddit(request.form) is not None: return jsonify(type='danger', text=util.validate_request_subreddit(request.form)) subreddit = request.form['subreddit'] include_comments = request.form['comments'] time = request.form['time'] limit = int(request.form['limit']) address = request.form['email'] kindle_address = request.form['kindle_address'] try: posts = util.get_posts(subreddit, time, limit) if time == 'all': title = 'Top ' + str(limit) + ' posts from /r/' + subreddit + ' ever' else: title = 'Top ' + str(limit) + ' posts from /r/' + subreddit + ' over the past ' + time top = [] for post in posts: author = '[deleted]' if post.author is None else post.author.name comments = None if include_comments == 'true': post.comments.replace_more(limit=0) comments = util.get_comments(post, request.form['comments_style'], author) try: top.append({'title': post.title, 'body': util.get_content(post.url) if not post.url.startswith( 'https://www.reddit.com/r/') else util.markdown( post.selftext), 'author': author, 'comments': comments}) except: pass except: return jsonify(type='danger', text='That ain\'t no subreddit I\'ve ever heard of!') attachment = render_template('posts.html', posts=top, title=title) status = util.send_email(address, kindle_address, attachment, title) if status is None: return jsonify(type='success', text='Success!') else: return jsonify(type='warning', text='Uh oh! Something went wrong on our end')
def get_all_source_ids(blog, original_blogs): #determine the number of posts bloginfo = util.get_bloginfo(blog, dict()) n_posts = bloginfo['posts'] print(n_posts, 'posts') n_calls = 1 + (n_posts // 20) #get IDs source_ids = set() for i in range(0, n_calls): offset = i * 20 posts = util.get_posts(blog, {'offset': offset}) print(len(posts), 'posts returned') for post in posts: source_id = get_source_id(post, original_blogs) if source_id is not None: source_ids.add(source_id) return source_ids
def home_page(): return render_template("index.html", posts=get_posts())
def rando(): posts = get_posts() r = random.randint(0, len(posts) - 1) return redirect(posts[r]['href'])
from flask import render_template from flask.ext.flatpages import pygments_style_defs from app import app, pages import util posts = util.get_posts() base = { 'most_recent': util.get_recent(posts) } @app.route('/') def home(): page = pages.get_or_404(posts[0].path) return render_template('page.html', page=page, base=base, nav='home') @app.route('/archives/') def archives(): return render_template('archives.html', pages=posts, base=base, nav='archives') @app.route('/blog/<path:path>/') def page(path): # Path is the filename of a page, without the file extension # e.g. "first-post" page = pages.get_or_404(path) return render_template('page.html', page=page, base=base, nav='archives')