Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import json
- import flask
- import os
- import yum.account as acc
- import yum.CONSTANTS as CONST
- from yum import Parser, food, state
- from yum.Guest import Guest
- from .cli import cli
- from click._unicodefun import click
- from flask import request
- class YumWeb(flask.Flask):
- """admin_username = None
- admin_password = None
- admin_hashtag = None
- admin_config = None
- food_list = []
- recipe_file = 'recipe.cfg'
- friends_file = 'friends.cfg'
- save_file = 'savegame.cfg'"""
- def __init__(self, *args, **kwargs):
- super().__init__(*args, **kwargs)
- def create_app():
- """
- Create the web application
- :return: The web application
- """
- print("creating app...")
- app = YumWeb(__name__)
- reload(app)
- return app
- def reload(app):
- config = setConfigFile()
- app.admin_username = Parser.getUsername(None, config)
- app.admin_password = Parser.getPassword(None, config)
- app.admin_hashtag = Parser.get(config, 'instagram', 'hashtag')
- app.admin_config = config
- app.recipe_file = CONST.RECIPE_FILE
- app.friends_file = CONST.FRIENDS_FILE
- app.save_file = CONST.SAVE_FILE
- app.food_list = acc.getAllFood(app.admin_username, app.admin_password, app.admin_hashtag)
- app.profile = acc.getProfile()
- def setConfigFile():
- if "YUM_CONFIG" in os.environ:
- return os.environ['YUM_CONFIG']
- else:
- return 'config.cfg'
- app = create_app()
- @app.route('/', methods=['GET', 'POST'])
- def index():
- method = request.method
- if method == 'GET':
- return processGet()
- @app.route('/login', methods=['POST'])
- def processGuestLogin():
- method = request.method
- if method == 'POST':
- username = request.form['username']
- password = request.form['password']
- foods = request.form.getlist("food")
- if(username != None and
- password != None and
- len(foods) != 0):
- # login and like
- yumFood(username, password, foods)
- # update food in friends list to yes
- for id in foods:
- if Parser.has_field(app.friends_file, username.lower(), id):
- Parser.updateSection(app.friends_file, username.lower(), id, 'yes')
- return flask.redirect('/',302)
- @app.route('/addfriend', methods=['POST'])
- def addRemoteFriend():
- method = request.method
- if(method == "POST"):
- j = json.loads(request.json)
- username = j['username']
- postID = j['post']
- Parser.updateSection(app.friends_file, username, postID, 'no')
- return "200"
- @app.route('/gain', methods=['POST'])
- def gain():
- method = request.method
- fields = [ 'level' , 'xp', 'likes' ]
- values = []
- if(method == "POST"):
- j = json.loads(request.json)
- for f in fields:
- values.append(j[f])
- Parser.updateSave(app.save_file, 'save', fields, values)
- return "200"
- @app.route('/saverecipe', methods=['POST'])
- def saveRecipe():
- method = request.method
- if(method == "POST"):
- j = json.loads(request.json)
- fields = ['name','url', 'ingredients','picture']
- values = []
- for f in fields:
- values.append(j[f])
- Parser.updateSave(app.recipe_file, 'recipe', fields, values)
- return "200"
- @cli.command()
- @click.pass_context
- @click.option('--host', '-h', default="127.0.0.1")
- @click.option('--port', '-p', default=5000)
- @click.option('--debug', '-d', is_flag=True)
- def run_server(ctx, host, port, debug):
- """print("run server")
- config = ctx.obj['config']
- username = Parser.getUsername(ctx.obj['username'], config)
- password = Parser.getPassword(ctx.obj['password'], config)
- hashtag = Parser.get(config, 'instagram', 'hashtag')
- app.admin_username = username
- app.admin_password = password
- app.admin_config = config
- app.admin_hashtag = hashtag
- app.food_list = acc.getAllFood(app.admin_username, app.admin_password, app.admin_hashtag)
- app.profile = acc.getProfile()
- app.recipe_file = ctx.obj['recipe_file']
- app.friends_file = ctx.obj['friends_file']
- app.save_file = ctx.obj['save_file']"""
- #reload(app)
- app.run(host,port,debug)
- def processGet():
- posts = []
- for food in app.food_list:
- posts.append(food.split(' '))
- lastFood = loadLastRecipe()
- user = state.State(0,False)
- ingredients = lastFood.ingredients
- return flask.render_template('index.html',
- posts=posts,
- profile = app.profile,
- last=lastFood,
- user=user,
- ingredients=ingredients)
- def loadLastRecipe():
- lastFood = food.Food(None)
- lastFood = lastFood.load(app.recipe_file)
- return lastFood
- def yumFood(username, password, foodList):
- guest = Guest(username, password)
- result = guest.login()
- if result != None:
- for food in foodList:
- guest.yum(food)
- guest.logout()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement