Advertisement
Guest User

Volpejper

a guest
Nov 20th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.63 KB | None | 0 0
  1. #!/bin/env python3
  2.  
  3. from datetime import datetime
  4. import requests
  5. import os
  6.  
  7. def timestamp_time(stamp):
  8.     return decimal_time(int(stamp[11:13]), int(stamp[14:16]), int(stamp[17:19]))
  9.  
  10. def current_time():
  11.  
  12.     h = int(datetime.now().strftime("%H"))
  13.     m = int(datetime.now().strftime("%M"))
  14.     s = int(datetime.now().strftime("%S"))
  15.     return decimal_time(h, m, s)
  16.  
  17. def decimal_time(h, m, s):
  18.  
  19.     return h + m / 60.0 + s / 3600.0
  20.  
  21. def location():
  22.  
  23.     ip = requests.get('http://ip-api.com/json').json()
  24.     latitude = ip['lat']
  25.     longitude = ip['lon']
  26.     return (latitude, longitude)
  27.  
  28. def sun(location):
  29.  
  30.     ip = requests.get('https://api.sunrise-sunset.org/json?lat=%s&lng=%s&formatted=0&date=today' % location).json()['results']
  31.     return {
  32.         'sunrise'   : timestamp_time(ip['sunrise']),
  33.         'noon'      : timestamp_time(ip['solar_noon']),
  34.         'sunset'    : timestamp_time(ip['sunset']),
  35.         'midnight'  : (timestamp_time(ip['solar_noon']) + 12) % 24
  36.     }
  37.  
  38. def points(sun):
  39.  
  40.     return [
  41.         sun['sunrise'] * 0.8,
  42.         sun['sunrise'],
  43.         sun['sunrise'] + (sun['noon'] - sun['sunrise']) * 0.3,
  44.         sun['noon'],
  45.         sun['noon'] + (sun['sunset'] - sun['noon']) * 0.9,
  46.         sun['sunset'],
  47.         sun['sunset'] + (sun['midnight'] - sun['sunset']) * 0.3,
  48.         sun['midnight']
  49.     ]
  50.  
  51. try:
  52.     points = points(sun(location()))
  53. except:
  54.     points = [5, 6, 7, 12, 19, 20, 21, 0]
  55.  
  56.  
  57. remaining = list(filter(lambda x: x[1] >= current_time(), enumerate(points)))
  58.  
  59. current = 7
  60. if len(remaining) > 0:
  61.     # current = max((remaining[0][0] - 1, 0))
  62.     current = remaining[0][0]
  63.  
  64. # print(points, remaining, current)
  65.  
  66. print(os.path.dirname(os.path.realpath(__file__)) + '/images/' + str(current) + '.png')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement