def generate(): meals = json.loads(secureData.file("mealExample.json", "/home/pi/Git/Tools/ExampleFiles")) ingredients = [] plannedMeals = [] # iterate through each meal set and write to file key_index = -1 for key in meals.keys(): key_index += 1 for i in range(7): day_w = (datetime.datetime(2021, 4, 29) + datetime.timedelta(days=i+1)).strftime("%A") # start the list for Friday. Change date to modify if(key_index == 0): plannedMeals.append({day_w: [{}, {}, {}]}) mod_i = i % len(meals[key]) if i < 7: plannedMeals[i][day_w][key_index] = {key: (meals[key][mod_i])} for ingredient in meals[key][mod_i]["ingredients"]: ingredients.append(ingredient) print("Generated.") ingredients = list(dict.fromkeys(ingredients)) secureData.write("WeeklyMeals.json", json.dumps(plannedMeals), "notes") secureData.appendUnique("Shopping.txt", '\n'.join(ingredients), "notes") print(f"Meals have been saved to {secureData.notesDir}WeeklyMeals.json.") print(f"Ingredients from WeeklyMeals.json have been added to {secureData.notesDir}Shopping.txt.") secureData.log("Generated meals, shopping list, and WeeklyMeals.json") ls(plannedMeals)
def ls(mealsFile=json.loads(secureData.file("WeeklyMeals.json", "notes"))): toWrite="" for day in range(7): toWrite += f"{list(mealsFile[day].keys())[0]}:\n" for mealType in range(0,3): meals = list(mealsFile[day].values())[0] toWrite += f" {list(meals[mealType].values())[0]['name']}\n" secureData.write("MealsList.txt", toWrite, "notes") print(f"A simplified meal list (without the ingredients) is available at {secureData.notesDir}MealsList.txt.") secureData.log("Generated MealsList.txt")
def renewAccessToken(): params = (('client_id', client_id), ('client_secret', client_secret), ('refresh_token', secureData.variable("NestRefreshToken")), ('grant_type', 'refresh_token')) response = requests.post('https://www.googleapis.com/oauth2/v4/token', params=params) response_json = response.json() print(response.json()) access_token = response_json['token_type'] + ' ' + response_json[ 'access_token'] print('Access token: ' + access_token) secureData.write("NestAccessToken", access_token)
currentIP = secureData.variable("currentIP") email = secureData.variable("email") print("Updating IP Address, " + email) ip = get('https://api.ipify.org?format=json').text discoveredIP = json.loads(ip)["ip"] print("Found " + discoveredIP + ", currently " + currentIP + ".") # IP was updated if (currentIP == discoveredIP): print("No change.") else: print("New IP! Updating and sending email.") secureData.write("currentIP", discoveredIP) # Replace OVPN file with new IP and email it newLine = "remote " + discoveredIP + " 1194" vpnFile = secureData.file("tyler.cloud.ovpn") vpnFile = vpnFile.split("remote ")[0] + newLine + vpnFile.split(" 1194")[1] secureData.write("tyler.cloud.ovpn", vpnFile) # Send email message = """ Your public IP was updated from %s to %s. To keep tyler.cloud, update your Namecheap settings.<br><br> To keep TylerVPN, please switch your OVPN file to the one located in SecureData.""" % ( currentIP, discoveredIP) mail.send("IP Updated - new OVPN file", message)
sunset = response["daily"][0]["sunset"] timeToSunset = (sunset - time.time()) / 3600 if(((temperature >= 65 and temperature <= 85) or (high >= 65 and high <=90)) and wind < 10 and timeToSunset > 2): message = f"""\ Hi Tyler,\ <br><br>Get walking, biking, and moving!<br><br> <ul> <li>It's {temperature}° right now with a high of {high}°- what a promising day to get outside!</li> <li>It's not raining</li> <li>The wind isn't bad</li> <li>It's a nice time of day</li> <br><br><h2><a href='{getBikeLink()}'>Here's a random place for you to go.</a></h2>""" mail.send("Get outside today, please!", message) secureData.write("walkAlertSent",str(int(time.time()))) secureData.log("Walk Alert Sent") # Planty Alerts if(int(secureData.variable("plantyAlertSent")) < (time.time() - 43200)): secureData.log("Checked Planty") if(low < 55 and plantyStatus == "out"): mail.send("Take Planty In", "Hi Tyler,<br><br>The low tonight is {}°. Please take Planty in!".format(low)) secureData.write("plantyStatus", "in") secureData.write("plantyAlertSent", str(int(time.time()))) if(high > 80 and plantyStatus == "in"): mail.send("Take Planty Out", "Hi Tyler,<br><br>It looks like a nice day! It's going to be around {}°. Please take Planty out.".format(high)) secureData.write("plantyStatus", "out") secureData.write("plantyAlertSent", str(int(time.time())))
return toReturn if __name__ == '__main__': client_credentials_manager = SpotifyClientCredentials() sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager) filePath = f"/var/www/html/Logs/Songs/{str(datetime.date.today())}.csv" f = open(filePath, 'w+') # parse playlist by ID results = sp.playlist(spotipy_playlist_id) tracks = results['tracks'] totalTracks = results['tracks']['total'] secureData.write("SPOTIPY_SONG_COUNT", str(totalTracks)) # go through each set of songs, 100 at a time (due to API limits) f.write(str(show_tracks(tracks))) while tracks['next']: tracks = sp.next(tracks) f.write(str(show_tracks(tracks))) print(f"Average Year: {str(mean(songYears))}") secureData.log("Updated Spotify Log") secureData.write("SPOTIPY_AVERAGE_YEAR", str(mean(songYears))) print("\n\nDone. Updating Git:") f.close() # Push the new log file (and anything else from today) to Github os.system("cd /var/www/html; git pull; git add -A; git commit -m 'Updated Logs'; git push")