def recipe(self): """ Support function to generate recipe """ api_id = Parser.get(self.config, 'yummly', 'api-id') api_key = Parser.get(self.config, 'yummly', 'api-key') server_url = Parser.get(self.config, 'server', 'url') yummer = yum.Yum(api_id, api_key, server_url) checkbox = self.w.findChild(QtWidgets.QCheckBox, 'checkConfig') file = False if checkbox.isChecked(): file = True print("choosing recipe...") result = yummer.recipe(diet=None, allergy=None, cuisine=None, exclude_cuisine=None, ingredient=None, exclude_ingredient=None, holiday=None, exclude_holiday=None, phrase=None, file=file, config=self.config) if result: print("reloading...") self.reloadRecipe()
def gain(ctx, id): """ Gain XP from Yum4FIT pictures on Instagram :param id: Returns the XP gain only from the picture with the ID set as an argument :return: XP Gain """ config = ctx.obj['config'] username = Parser.getUsername(ctx.obj['username'], config) password = Parser.getPassword(ctx.obj['password'], config) hashtag = Parser.get(config, 'instagram', 'hashtag') friendsFile = ctx.obj['friends_file'] serverURL = ctx.obj['server_url'] setConfirmed(password, serverURL) result = acc.gain(username, password, hashtag, id, friendsFile) if not id: data = { 'likes': str(result.likes), 'level': str(result.level), 'xp': str(result.xp) } json_data = json.dumps(data) url = ctx.obj['server_url'] url += 'gain' connector.post(url, json_data, password) # result is state if (id == None): click.echo(result.text()) # result is likes count else: click.echo("%s: " % id + "%d likes" % result + " (%s XP)" % (result * CONST.XP_FACTOR))
def recipe(ctx, diet, allergy, cuisine, exclude_cuisine, ingredient, exclude_ingredient, holiday, exclude_holiday, phrase, file): """ Generate recipes with difficulty computed from your level :param diet: Diets :param allergy: Allergies :param cuisine: National cuissines :param exclude_cuisine: Excluding national cuisines :param ingredient: Ingredients :param exclude_ingredient: Excluding ingredients :param holiday: Holiday recipes :param exclude_holiday: Excluding holiday recipes :param phrase: Name of the recipe :param file: Config file """ config = ctx.obj['config'] api_id = Parser.get(config, 'yummly', 'api-id') api_key = Parser.get(config, 'yummly', 'api-key') server_url = Parser.get(config, 'server', 'url') yummer = yum.Yum(api_id, api_key, server_url) result = yummer.recipe(diet, allergy, cuisine, exclude_cuisine, ingredient, exclude_ingredient, holiday, exclude_holiday, phrase, file, config) if result: print(result.text())
def save(self): """ Saving the game - writing the state to file savegame.cfg """ fields = [ 'level' , 'xp', 'likes' ] values = [ str(self.level), str(self.xp), str(self.likes) ] Parser.updateSave(CONST.SAVE_FILE, 'save', fields, values)
def add_friend(ctx, username, id): data = {"username": username, "post": id} json_data = json.dumps(data) url = ctx.obj['server_url'] url += 'addfriend' Parser.updateSection(CONST.FRIENDS_FILE, username, id, 'no') config = ctx.obj['config'] password = Parser.getPassword(ctx.obj['password'], config) connector.post(url, json_data, password)
def login(self): """ Login to Instagram """ username = Parser.getUsername(None, self.config) password = Parser.getPassword(None, self.config) account.login(username, password) self.profile = account.getProfile() self.password = password self.hashtag = Parser.get(self.config, 'instagram', 'hashtag')
def load(self, file): """ Load food from recipe file - last generated user's food :param file: recipe file (default: recipe.cfg) :return: Food """ self.name = Parser.get(file, 'recipe', 'name') self.ingredients = Parser.get(file, 'recipe', 'ingredients') self.url = Parser.get(file, 'recipe', 'url') self.picture = Parser.get(file, 'recipe', 'picture') return self
def share(ctx, path, caption): """ Share photo on instagram :param path: Path to the picture :param caption: Caption of picture on instagram """ config = ctx.obj['config'] username = Parser.getUsername(ctx.obj['username'], config) password = Parser.getPassword(ctx.obj['password'], config) hashtag = Parser.get(config, 'instagram', 'hashtag') serverURL = ctx.obj['server_url'] acc.upload(username, password, path, caption + " " + hashtag, serverURL)
def addRemoteFriend(): """ Processing POST method from local for adding friend on the server side :return: Code 200 if ok """ method = request.method if (method == "POST" and checkPassword(request, app.admin_password)): j = json.loads(request.json) username = j['username'] postID = j['post'] Parser.updateSection(app.friends_file, username, postID, 'no') return "200"
def food(ctx): """ Print all the Yum4FIT food on your instagram :param ctx: :return: """ config = ctx.obj['config'] username = Parser.getUsername(ctx.obj['username'], config) password = Parser.getPassword(ctx.obj['password'], config) hashtag = Parser.get(config, 'instagram', 'hashtag') food = acc.getAllFood(username, password, hashtag) for actual in food: print(actual)
def load(self): """ Loading the state from savegame.cfg file :return: """ # check the state file here if not Parser.exists(CONST.SAVE_FILE): self.initState() self.likes = int( Parser.get(CONST.SAVE_FILE, 'save', 'likes') ) self.xp = int( Parser.get(CONST.SAVE_FILE, 'save', 'xp') ) self.level = int( Parser.get(CONST.SAVE_FILE, 'save', 'level') ) return self
def gain(): """ Processing POST method from local for gaining XP points on the server side :return: """ method = request.method fields = ['level', 'xp', 'likes'] values = [] if (method == "POST" and checkPassword(request, app.admin_password)): j = json.loads(request.json) for f in fields: values.append(j[f]) Parser.updateSave(app.save_file, 'save', fields, values) return "200"
def share(self): """ Support function for sharing photos of the food. Includes opening file dialog and setting caption :return: """ file = self.open() if file != None: caption = self.setCaption() if caption == None: caption = "" hashtag = Parser.get(self.config, 'instagram', 'hashtag') server_url = Parser.get(self.config, 'server', 'url') account.upload(self.profile.username, self.password, file, caption + " " + hashtag, server_url) self.loadFood()
def saveRecipe(): """ Processing POST method from local for saving the new generated recipe :return: """ method = request.method if (method == "POST" and checkPassword(request, app.admin_password)): 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"
def saveRecipe(self, food, config): """ Save the recipe to recipe.cfg file :param food: food to save :param config: configuration file """ fields = ['name', 'url', 'ingredients', 'picture'] ingredients = ",".join(food.ingredients) values = [food.name, food.url, ingredients, food.picture] jsonData = self.buildJson(fields, values) url = self.server url += 'saverecipe' password = Parser.getPassword(None, config) connector.post(url, jsonData, password) Parser.updateSave(CONST.RECIPE_FILE, 'recipe', fields, values)
def cli(ctx, config, username, password): ctx.obj['config'] = config ctx.obj['username'] = username ctx.obj['password'] = password ctx.obj['recipe_file'] = CONST.RECIPE_FILE ctx.obj['save_file'] = CONST.SAVE_FILE ctx.obj['friends_file'] = CONST.FRIENDS_FILE ctx.obj['server_url'] = Parser.get(config, 'server', 'url')
def reload(app): """ Reload the application data :param app: target application :return: reloaded application """ print("reloading 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 addFriend(self): """ Support function to add friend using the item chosen by clicking to listWidget of foods """ line = self.w.findChild(QtWidgets.QLineEdit, 'lineFriend') friend = line.text() if (friend == None or friend == ""): return else: listView = self.w.findChild(QtWidgets.QListWidget, 'listFoods') id = listView.currentItem().text() if id: data = {"username": friend, "post": id} json_data = json.dumps(data) url = Parser.get(self.config, 'server', 'url') url += 'addfriend' Parser.updateSection(CONSTANTS.FRIENDS_FILE, friend, id, 'no') connector.post(url, json_data, self.password)
def setConfirmed(password, url): response = connector.post(url + 'confirmated', "", password) parser = Parser.parse(CONST.FRIENDS_FILE) friends = parser.sections() if not response: return data = json.loads(response.text) sections = [] fields = [] for local in friends: for new in data: extern = new.split('--')[0] if extern == local: sections.append(local) fields.append(data[new]) for s in sections: for f in fields: Parser.updateSection(CONST.FRIENDS_FILE, s, f, 'yes')
def loadParamsFromFile(self, config): """ Load user's parameters from the configuration file :param config: path to config file """ diet = Parser.getParameter(config, 'diet') allergy = Parser.getParameter(config, 'allergy') cuisine = Parser.getParameter(config, 'cuisine') exclude_cuisine = Parser.getParameter(config, 'exclude_cuisine') ingredient = Parser.getParameter(config, 'ingredient') exclude_ingredient = Parser.getParameter(config, 'exclude_ingredient') holiday = Parser.getParameter(config, 'holiday') exclude_holiday = Parser.getParameter(config, 'exclude_holiday') phrase = Parser.getParameter(config, 'phrase') self.setAllParameters(diet, allergy, cuisine, exclude_cuisine, ingredient, exclude_ingredient, holiday, exclude_holiday, phrase, False, config)
def processGuestLogin(): """ Process guest login and sending the yum Yum is representation of like and agreeing with sharing food with host :return: redirection back """ 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)
def gain(self): """ Support function to gain XP from likes """ state = account.gain(self.profile.username, self.password, self.hashtag, None, CONSTANTS.FRIENDS_FILE) data = { 'likes': str(state.likes), 'level': str(state.level), 'xp': str(state.xp) } json_data = json.dumps(data) url = Parser.get(self.config, 'server', 'url') url += 'gain' connector.post(url, json_data, self.password) self.loadState()
def sumLikes(allMedia, hashtag, friendsFile): """ Summary of all likes on Yum4FIT posts :param allMedia: All your Instagram posts :param hashtag: The specific hashtag for Yum4FIT :return: The summary of all likes on Yum4FIT posts """ summary = 0 # all together #print(json.dumps(allMedia)) for post in allMedia: if (post['caption'] != None): if (hashtag in post['caption']['text']): likes_count = post['like_count'] id = post['id'] for liker in post['likers']: usr = liker['username'] agreed = Parser.get(friendsFile, usr, id) if agreed == 'yes': likes_count += 4 summary += int(likes_count) user = state.State(summary, True) return user
def initState(self): fields = [ 'level' , 'xp', 'likes' ] values = [ '0', '0', '0' ] Parser.updateSave(CONST.SAVE_FILE, 'save', fields, values)
def getConfirmated(config): friends = Parser.getConfirmated(CONST.FRIENDS_FILE) return produceJson(friends)