def random_tweet(): tweet = TwitterAPI.get_random_tweet() tweet_id = tweet['id'] # Store the random tweet with open('tweets/%d.json' % tweet_id, 'w') as f: f.write(json.dumps(tweet)) # Get our additional information query = Query(tweet['text'] + " " + tweet['user']['name']) query.run() article = query.get_result_article() article_file_name = query.get_result_filename() # Replace all the links in the article if article is not None: article = article.replace('/wiki/', 'https://en.wikipedia.org/wiki/') if article is not None: markup = TwitterAPI.get_tweet_markup(tweet_id) parameters = { 'article_html': article, 'article_filename': article_file_name, 'tweet_html': markup, 'ratings': [1, 2, 3, 4, 5], 'tweet_id': tweet_id, } return render_template('tweet.html', **parameters) else: # We need to find another tweet return random_tweet()