def compose(): if not session.get('id'): session_id = sessions.createSession(dbconfig) session['id'] = session_id title = request.form.get("poemTitle", None) if title is None: raise InvalidAPIUsage("No article title provided", "You must provide a Wikipedia article title to get a poem") twitterHandle = request.form.get("twitterHandle", None, type=str) page_id = articles.getArticleIdForTitle(dbconfig, title) if page_id is None: raise InvalidAPIUsage("Article not found", "Could not find a Wikipedia article with title {}".format(title)) poem_dict = poems.getCachedPoemForArticle(dbconfig, page_id, True, session['id']) if poem_dict is not None: if 'id' in session: sessions.addPoemToSession(dbconfig, poem_dict['id'], session['id']) print_poem(page_id, poem_dict) if poem_dict is None: poem_dict = poems.getCachedPoemForArticle(dbconfig, page_id, False, session['id']) if poem_dict is None: userdata = {} if twitterHandle is not None: userdata = {"source":"twitter", "session":session["id"], "twitter_handle":twitterHandle} task_count = tasks.getTaskCountForTwitterHandle(dbconfig, twitterHandle, datetime.now()-timedelta(hours=1)) elif session.get('id'): userdata = {"source":"website", "session":session["id"]} task_count = tasks.getTaskCountForSession(dbconfig, session['id'], datetime.now()-timedelta(hours=1)) if not userdata: raise InvalidAPIUsage("Sessions inactive", "The Wikisonnet API requires that sessions be active to work") if task_count >= RATE_LIMIT: raise InvalidAPIUsage("Rate limit exceeded", "Because Wikisonnet is run by struggling artists, we can only support {} poems per user per hour".format(RATE_LIMIT), status_code=429) poem_dict = poems.writeNewPoemForArticle(dbconfig, page_id, task_condition, userdata) return jsonify(poem_dict)
def compose(): if not session.get('id'): session_id = sessions.createSession(dbconfig) session['id'] = session_id title = request.form.get("poemTitle", None) if title is None: raise InvalidAPIUsage( "No article title provided", "You must provide a Wikipedia article title to get a poem") twitterHandle = request.form.get("twitterHandle", None, type=str) page_id = articles.getArticleIdForTitle(dbconfig, title) if page_id is None: raise InvalidAPIUsage( "Article not found", "Could not find a Wikipedia article with title {}".format(title)) poem_dict = poems.getCachedPoemForArticle(dbconfig, page_id, True, session['id']) if poem_dict is not None: if 'id' in session: sessions.addPoemToSession(dbconfig, poem_dict['id'], session['id']) print_poem(page_id, poem_dict) if poem_dict is None: poem_dict = poems.getCachedPoemForArticle(dbconfig, page_id, False, session['id']) if poem_dict is None: userdata = {} if twitterHandle is not None: userdata = { "source": "twitter", "session": session["id"], "twitter_handle": twitterHandle } task_count = tasks.getTaskCountForTwitterHandle( dbconfig, twitterHandle, datetime.now() - timedelta(hours=1)) elif session.get('id'): userdata = {"source": "website", "session": session["id"]} task_count = tasks.getTaskCountForSession( dbconfig, session['id'], datetime.now() - timedelta(hours=1)) if not userdata: raise InvalidAPIUsage( "Sessions inactive", "The Wikisonnet API requires that sessions be active to work") if task_count >= RATE_LIMIT: raise InvalidAPIUsage( "Rate limit exceeded", "Because Wikisonnet is run by struggling artists, we can only support {} poems per user per hour" .format(RATE_LIMIT), status_code=429) poem_dict = poems.writeNewPoemForArticle(dbconfig, page_id, task_condition, userdata) return jsonify(poem_dict)
def lookup(poem_id): if not session.get('id'): session_id = sessions.createSession(dbconfig) session['id'] = session_id options = createOptions() poem_dict = poems.getSpecificPoem(dbconfig, poem_id, session['id'], options) if poem_dict is not None and poem_dict['complete']: if 'id' in session: print(session.get('id')) print("adding poem to session in get") sessions.addPoemToSession(dbconfig, poem_dict['id'], session['id']) print_poem(poem_dict['starting_page'], poem_dict) elif poem_dict is None: return not_found() return jsonify(poem_dict)