Advertisement
Guest User

Untitled

a guest
Feb 7th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.06 KB | None | 0 0
  1. import json
  2. import flask
  3. import os
  4. import yum.account as acc
  5. import yum.CONSTANTS as CONST
  6.  
  7. from yum import Parser, food, state
  8. from yum.Guest import Guest
  9. from .cli import cli
  10. from click._unicodefun import click
  11. from flask import request
  12.  
  13. class YumWeb(flask.Flask):
  14. """admin_username = None
  15. admin_password = None
  16. admin_hashtag = None
  17. admin_config = None
  18. food_list = []
  19. recipe_file = 'recipe.cfg'
  20. friends_file = 'friends.cfg'
  21. save_file = 'savegame.cfg'"""
  22.  
  23. def __init__(self, *args, **kwargs):
  24. super().__init__(*args, **kwargs)
  25.  
  26. def create_app():
  27. """
  28. Create the web application
  29. :return: The web application
  30. """
  31. print("creating app...")
  32. app = YumWeb(__name__)
  33. reload(app)
  34. return app
  35.  
  36. def reload(app):
  37. config = setConfigFile()
  38. app.admin_username = Parser.getUsername(None, config)
  39. app.admin_password = Parser.getPassword(None, config)
  40. app.admin_hashtag = Parser.get(config, 'instagram', 'hashtag')
  41. app.admin_config = config
  42. app.recipe_file = CONST.RECIPE_FILE
  43. app.friends_file = CONST.FRIENDS_FILE
  44. app.save_file = CONST.SAVE_FILE
  45. app.food_list = acc.getAllFood(app.admin_username, app.admin_password, app.admin_hashtag)
  46. app.profile = acc.getProfile()
  47.  
  48. def setConfigFile():
  49. if "YUM_CONFIG" in os.environ:
  50. return os.environ['YUM_CONFIG']
  51. else:
  52. return 'config.cfg'
  53.  
  54. app = create_app()
  55. @app.route('/', methods=['GET', 'POST'])
  56. def index():
  57. method = request.method
  58. if method == 'GET':
  59. return processGet()
  60.  
  61. @app.route('/login', methods=['POST'])
  62. def processGuestLogin():
  63. method = request.method
  64. if method == 'POST':
  65. username = request.form['username']
  66. password = request.form['password']
  67. foods = request.form.getlist("food")
  68.  
  69. if(username != None and
  70. password != None and
  71. len(foods) != 0):
  72. # login and like
  73. yumFood(username, password, foods)
  74.  
  75. # update food in friends list to yes
  76. for id in foods:
  77. if Parser.has_field(app.friends_file, username.lower(), id):
  78. Parser.updateSection(app.friends_file, username.lower(), id, 'yes')
  79.  
  80. return flask.redirect('/',302)
  81.  
  82. @app.route('/addfriend', methods=['POST'])
  83. def addRemoteFriend():
  84. method = request.method
  85. if(method == "POST"):
  86. j = json.loads(request.json)
  87. username = j['username']
  88. postID = j['post']
  89. Parser.updateSection(app.friends_file, username, postID, 'no')
  90. return "200"
  91.  
  92. @app.route('/gain', methods=['POST'])
  93. def gain():
  94. method = request.method
  95. fields = [ 'level' , 'xp', 'likes' ]
  96. values = []
  97. if(method == "POST"):
  98. j = json.loads(request.json)
  99. for f in fields:
  100. values.append(j[f])
  101. Parser.updateSave(app.save_file, 'save', fields, values)
  102. return "200"
  103.  
  104. @app.route('/saverecipe', methods=['POST'])
  105. def saveRecipe():
  106. method = request.method
  107. if(method == "POST"):
  108. j = json.loads(request.json)
  109. fields = ['name','url', 'ingredients','picture']
  110. values = []
  111. for f in fields:
  112. values.append(j[f])
  113. Parser.updateSave(app.recipe_file, 'recipe', fields, values)
  114.  
  115. return "200"
  116.  
  117. @cli.command()
  118. @click.pass_context
  119. @click.option('--host', '-h', default="127.0.0.1")
  120. @click.option('--port', '-p', default=5000)
  121. @click.option('--debug', '-d', is_flag=True)
  122. def run_server(ctx, host, port, debug):
  123. """print("run server")
  124. config = ctx.obj['config']
  125. username = Parser.getUsername(ctx.obj['username'], config)
  126. password = Parser.getPassword(ctx.obj['password'], config)
  127. hashtag = Parser.get(config, 'instagram', 'hashtag')
  128.  
  129. app.admin_username = username
  130. app.admin_password = password
  131. app.admin_config = config
  132. app.admin_hashtag = hashtag
  133. app.food_list = acc.getAllFood(app.admin_username, app.admin_password, app.admin_hashtag)
  134. app.profile = acc.getProfile()
  135. app.recipe_file = ctx.obj['recipe_file']
  136. app.friends_file = ctx.obj['friends_file']
  137. app.save_file = ctx.obj['save_file']"""
  138.  
  139. #reload(app)
  140. app.run(host,port,debug)
  141.  
  142.  
  143. def processGet():
  144. posts = []
  145. for food in app.food_list:
  146. posts.append(food.split(' '))
  147.  
  148. lastFood = loadLastRecipe()
  149. user = state.State(0,False)
  150. ingredients = lastFood.ingredients
  151. return flask.render_template('index.html',
  152. posts=posts,
  153. profile = app.profile,
  154. last=lastFood,
  155. user=user,
  156. ingredients=ingredients)
  157.  
  158. def loadLastRecipe():
  159. lastFood = food.Food(None)
  160. lastFood = lastFood.load(app.recipe_file)
  161. return lastFood
  162.  
  163. def yumFood(username, password, foodList):
  164. guest = Guest(username, password)
  165. result = guest.login()
  166. if result != None:
  167. for food in foodList:
  168. guest.yum(food)
  169. guest.logout()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement