Guest User

Untitled

a guest
May 8th, 2020
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.03 KB | None | 0 0
  1. import ctypes
  2. import datetime
  3. import os
  4. import time
  5. from datetime import date
  6.  
  7. import requests
  8.  
  9.  
  10. def set_wallpaper(wallpaper_path=None):
  11.     ctypes.windll.user32.SystemParametersInfoW(SPI_SETDESKWALLPAPER, 0, wallpaper_path, 3)
  12.  
  13.  
  14. def get_wallpaper_key(timestamp, current_weather):
  15.     if date.today().weekday() == 6:
  16.         return "Sunday"
  17.     if current_weather == "Rain":
  18.         return "Rain"
  19.     elif current_weather == "Thunderstorm":
  20.         return "Storm"
  21.     elif current_weather == "Drizzle":
  22.         return "Drizzle"
  23.     elif current_weather == "Clear" and START_NIGHT <= timestamp or timestamp <= END_NIGHT:
  24.         return 'Night'
  25.     elif current_weather == "Clear" and START_DAY <= timestamp <= END_DAY:
  26.         return "Day"
  27.     elif current_weather == "Clouds":
  28.         return "Clouds"
  29.     else:
  30.         return "Other"
  31.  
  32.  
  33. SPI_SETDESKWALLPAPER = 20
  34. WALLPAPERS_DIR = r"C:\Users\Simon\Documents\Folders\Photos\Backgrounds\Python"
  35.  
  36. API_ADDRESS = 'http://api.openweathermap.org/data/2.5/weather?appid=0c42f7f6b53b244c78a418f4f181282a&q='
  37. CITY = "WHERE YOU LIVE"
  38. WEATHER_REFRESH_RATE = 120
  39. COMPLETE_API_URL = f'{API_ADDRESS}{CITY}'
  40.  
  41. START_NIGHT = datetime.time(18, 1)
  42. END_NIGHT = datetime.time(6, 0)
  43. START_DAY = datetime.time(6, 1)
  44. END_DAY = datetime.time(18, 0)
  45.  
  46. weather_wallpaper_filename_dict = {
  47.     'Sunday': 'Tokyo.jpg',
  48.     'Rain': 'Waterfall.jpg',
  49.     'Storm': 'Cloud.jpg',
  50.     'Drizzle': 'Rooftop1.png',
  51.     'Night': 'Moon.png',
  52.     'Day': 'KatanaMorning.png',
  53.     'Clouds': 'Shibuya.png',
  54.     'Other': 'Firewatch.jpg'
  55. }
  56.  
  57. if __name__ == '__main__':
  58.     while True:
  59.         timestamp = datetime.datetime.now().time()
  60.         json_data = requests.get(COMPLETE_API_URL).json()
  61.         format_data = json_data["weather"][0]["main"]
  62.         wallpaper_key = get_wallpaper_key(timestamp=timestamp, current_weather=format_data)
  63.         wallpaper_path = os.path.join(WALLPAPERS_DIR, weather_wallpaper_filename_dict[wallpaper_key])
  64.         set_wallpaper(wallpaper_path)
  65.         time.sleep(WEATHER_REFRESH_RATE)
Add Comment
Please, Sign In to add comment