mrmeval

Hacked kobo weather script generate a jpeg for other uses.

Nov 3rd, 2013
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.42 KB | None | 0 0
  1. import urllib2
  2. import pygame, os
  3.  
  4. from xml.dom.minidom import parseString
  5. from datetime import date, datetime, timedelta
  6. from subprocess import call
  7.  
  8. os.environ['SDL_NOMOUSE'] = '1'
  9. print("Kobo Wifi weather forecast started.")
  10.  
  11. def index_error(error):
  12. print(error)
  13. print("Failed to fetch weather data.")
  14. print("Double check your location settings by running:")
  15. print(" cat /mnt/onboard/.apps/koboWeather/location")
  16. print("If the information is incorrect, re-set your location with:")
  17. print(" /mnt/onboard/.apps/koboWeather/set_location.sh")
  18.  
  19. def to_hex(color):
  20. hex_chars = "0123456789ABCDEF"
  21. return hex_chars[color / 16] + hex_chars[color % 16]
  22.  
  23. def convert_to_raw(surface):
  24. print("Converting image . . .")
  25. raw_img = ""
  26. for row in range(surface.get_height()):
  27. for col in range(surface.get_width()):
  28. color = surface.get_at((col, row))[0]
  29. raw_img += ('\\x' + to_hex(color)).decode('string_escape')
  30. f = open("tmp/img.raw", "wb")
  31. f.write(raw_img)
  32. f.close()
  33. pygame.image.save(surface, "current_weather.jpg")
  34. print("Image converted.")
  35.  
  36.  
  37. def get_weather_data():
  38.  
  39. print("Getting weather information . . .")
  40.  
  41. try:
  42. location = open("location", "r")
  43. lat = location.readline().strip()
  44. lon = location.readline().strip()
  45. location.close()
  46. except IOError:
  47. print("\nCouldn't open location file.")
  48. print("Run the 'set_location.sh' script to set your location for the weather script.")
  49. return 1
  50. print(lat, lon)
  51. weather_link='http://api.worldweatheronline.com/free/v1/weather.ashx?q=needslocation&format=xml&num_of_days=5&key=needyourownkeuy'
  52. #print(weather_link)
  53.  
  54. weather_xml = urllib2.urlopen(weather_link)
  55. weather_data = weather_xml.read()
  56. weather_xml.close()
  57.  
  58. dom = parseString(weather_data)
  59.  
  60. unit_file = open("unit.txt", "r")
  61. unit = unit_file.read()
  62. unit_file.close()
  63. unit = unit.strip().upper()
  64.  
  65. h_temps = dom.getElementsByTagName('tempMax%s' % unit)
  66. l_temps = dom.getElementsByTagName('tempMin%s' % unit)
  67. highs = []
  68. lows = []
  69. for i in h_temps:
  70. try:
  71. highs.append(str(i.firstChild.nodeValue))
  72. except AttributeError as error:
  73. print("Error getting temperature highs: " + str(error))
  74.  
  75. for i in l_temps:
  76. try:
  77. lows.append(str(i.firstChild.nodeValue))
  78. except AttributeError as error:
  79. print("Error getting temperature lows: " + str(error))
  80.  
  81.  
  82. conditions = []
  83. for con in dom.getElementsByTagName('weatherDesc')[1:]:
  84. conditions.append(str(con.firstChild.nodeValue))
  85.  
  86. now = datetime.now()
  87. day3 = now + timedelta(days=2)
  88. day4 = now + timedelta(days=3)
  89. day5 = now + timedelta(days=4)
  90. days = ["Today", "Tomorrow", day3.strftime("%A"), day4.strftime("%A"), day5.strftime("%A")]
  91.  
  92. # images
  93. # The first image link is for the current weather, which we don't want.
  94. icons = dom.getElementsByTagName('weatherIconUrl')[1:]
  95. img_links = []
  96. for i in icons:
  97. try:
  98. link = "icons/" + str(i.firstChild.nodeValue)[72:]
  99. except AttributeError as error:
  100. print("Error getting icon links: " + str(error))
  101. img_links.append(link)
  102.  
  103.  
  104. #print(img_links)
  105. #print(highs, lows)
  106. #print(conditions)
  107. #print(days)
  108.  
  109. display(days, highs, lows, conditions, img_links, unit)
  110.  
  111.  
  112. def display(days, highs, lows, conditions, img_links, unit):
  113.  
  114. print("Creating image . . .")
  115.  
  116. #pygame.display.init()
  117. pygame.font.init()
  118. #pygame.mouse.set_visible(False)
  119.  
  120. white = (255, 255, 255)
  121. black = (0, 0, 0)
  122. gray = (125, 125, 125)
  123.  
  124. screen = pygame.Surface((600, 800))
  125. screen.fill(white)
  126.  
  127. tiny_font = pygame.font.Font("Cabin-Regular.ttf", 18)
  128. small_font = pygame.font.Font("Fabrica.otf", 22)
  129. font = pygame.font.Font("Forum-Regular.ttf", 40)
  130. comfortaa = pygame.font.Font("Comfortaa-Regular.ttf", 60)
  131. comfortaa_small = pygame.font.Font("Comfortaa-Regular.ttf", 35)
  132.  
  133. # Dividing lines
  134. pygame.draw.line(screen, gray, (10, 200), (590, 200))
  135. pygame.draw.line(screen, gray, (10, 400), (590, 400))
  136. pygame.draw.line(screen, gray, (200, 410), (200, 790))
  137. pygame.draw.line(screen, gray, (400, 410), (400, 790))
  138.  
  139. # Today
  140. date = small_font.render(days[0], True, black, white)
  141. date_rect = date.get_rect()
  142. date_rect.topleft = 10, 15
  143.  
  144. high = small_font.render('high: ', True, black, white)
  145. high_rect = high.get_rect()
  146. high_temp = comfortaa.render(highs[0], True, black, white)
  147. htemp_rect = high_temp.get_rect()
  148. high_rect.topleft = (50, 100)
  149. htemp_rect.topleft = high_rect.topright
  150.  
  151. low = small_font.render("low: ", True, black, white)
  152. low_rect = low.get_rect()
  153. low_rect.topleft = (400, 100)
  154. low_temp = comfortaa.render(lows[0], True, black, white)
  155. ltemp_rect = low_temp.get_rect()
  156. ltemp_rect.topleft = low_rect.topright
  157.  
  158.  
  159. condition = font.render(conditions[0], True, black, white)
  160. con_rect = condition.get_rect()
  161. con_rect.centerx = 300
  162. con_rect.top = 5
  163. # Make sure words don't overlap
  164. if con_rect.left < date_rect.right:
  165. con_rect.left = date_rect.right
  166.  
  167. image = pygame.image.load(img_links[0])
  168. image.set_colorkey((255, 255, 255))
  169. img_rect = image.get_rect()
  170. img_rect.center = (300, 125)
  171. degrees = pygame.image.load("icons/%s.png" % unit)
  172.  
  173. screen.blit(condition, con_rect)
  174. screen.blit(high, high_rect)
  175. screen.blit(degrees, htemp_rect.topright)
  176. screen.blit(degrees, ltemp_rect.topright)
  177. screen.blit(high_temp, htemp_rect)
  178. screen.blit(low, low_rect)
  179. screen.blit(low_temp, ltemp_rect)
  180. screen.blit(image, img_rect)
  181. screen.blit(date, date_rect)
  182.  
  183.  
  184. # Tomorrow
  185. date = small_font.render(days[1], True, black, white)
  186. date_rect = date.get_rect()
  187. date_rect.topleft = 10, 210
  188.  
  189. high = small_font.render('high: ', True, black, white)
  190. high_rect = high.get_rect()
  191. high_temp = comfortaa.render(highs[1], True, black, white)
  192. htemp_rect = high_temp.get_rect()
  193. high_rect.topleft = (50, 300)
  194. htemp_rect.topleft = high_rect.topright
  195.  
  196. low = small_font.render("low: ", True, black, white)
  197. low_rect = low.get_rect()
  198. low_rect.topleft = (400, 300)
  199. low_temp = comfortaa.render(lows[1], True, black, white)
  200. ltemp_rect = low_temp.get_rect()
  201. ltemp_rect.topleft = low_rect.topright
  202.  
  203.  
  204. condition = font.render(conditions[1], True, black, white)
  205. con_rect = condition.get_rect()
  206. con_rect.centerx = 300
  207. con_rect.top = 210
  208. if con_rect.left < date_rect.right:
  209. con_rect.left = date_rect.right
  210.  
  211. image = pygame.image.load(img_links[1])
  212. image.set_colorkey((255, 255, 255))
  213. img_rect = image.get_rect()
  214. img_rect.center = (300, 325)
  215.  
  216. screen.blit(condition, con_rect)
  217. screen.blit(high, high_rect)
  218. screen.blit(degrees, htemp_rect.topright)
  219. screen.blit(degrees, ltemp_rect.topright)
  220. screen.blit(high_temp, htemp_rect)
  221. screen.blit(low, low_rect)
  222. screen.blit(low_temp, ltemp_rect)
  223. screen.blit(image, img_rect)
  224. screen.blit(date, date_rect)
  225.  
  226. # Day 3
  227. date = small_font.render(days[2], True, black, white)
  228. date_rect = date.get_rect()
  229. date_rect.centerx = 100
  230. date_rect.top = 410
  231.  
  232. high = small_font.render('high: ', True, black, white)
  233. high_rect = high.get_rect()
  234. high_temp = comfortaa_small.render(highs[2], True, black, white)
  235. htemp_rect = high_temp.get_rect()
  236. high_rect.topright = (100, 630)
  237. htemp_rect.midleft = high_rect.midright
  238.  
  239. low = small_font.render("low: ", True, black, white)
  240. low_rect = low.get_rect()
  241. low_rect.topright = (100, 710)
  242. low_temp = comfortaa_small.render(lows[2], True, black, white)
  243. ltemp_rect = low_temp.get_rect()
  244. ltemp_rect.midleft = low_rect.midright
  245.  
  246.  
  247. condition = tiny_font.render(conditions[2], True, black, white)
  248. con_rect = condition.get_rect()
  249. con_rect.centerx = 100
  250. con_rect.top = 450
  251.  
  252. image = pygame.image.load(img_links[2])
  253. image.set_colorkey((255, 255, 255))
  254. img_rect = image.get_rect()
  255. img_rect.center = (100, 540)
  256.  
  257. screen.blit(condition, con_rect)
  258. screen.blit(high, high_rect)
  259. screen.blit(degrees, htemp_rect.topright)
  260. screen.blit(degrees, ltemp_rect.topright)
  261. screen.blit(high_temp, htemp_rect)
  262. screen.blit(low, low_rect)
  263. screen.blit(low_temp, ltemp_rect)
  264. screen.blit(image, img_rect)
  265. screen.blit(date, date_rect)
  266.  
  267.  
  268.  
  269. # Day 4
  270. date = small_font.render(days[3], True, black, white)
  271. date_rect = date.get_rect()
  272. date_rect.centerx = 300
  273. date_rect.top = 410
  274.  
  275. high = small_font.render('high: ', True, black, white)
  276. high_rect = high.get_rect()
  277. high_temp = comfortaa_small.render(highs[3], True, black, white)
  278. htemp_rect = high_temp.get_rect()
  279. high_rect.topright = (300, 630)
  280. htemp_rect.midleft = high_rect.midright
  281.  
  282. low = small_font.render("low: ", True, black, white)
  283. low_rect = low.get_rect()
  284. low_rect.topright = (300, 710)
  285. low_temp = comfortaa_small.render(lows[3], True, black, white)
  286. ltemp_rect = low_temp.get_rect()
  287. ltemp_rect.midleft = low_rect.midright
  288.  
  289.  
  290. condition = tiny_font.render(conditions[3], True, black, white)
  291. con_rect = condition.get_rect()
  292. con_rect.centerx = 300
  293. con_rect.top = 450
  294.  
  295. image = pygame.image.load(img_links[3])
  296. image.set_colorkey((255, 255, 255))
  297. img_rect = image.get_rect()
  298. img_rect.center = (300, 540)
  299.  
  300. screen.blit(condition, con_rect)
  301. screen.blit(high, high_rect)
  302. screen.blit(degrees, htemp_rect.topright)
  303. screen.blit(degrees, ltemp_rect.topright)
  304. screen.blit(high_temp, htemp_rect)
  305. screen.blit(low, low_rect)
  306. screen.blit(low_temp, ltemp_rect)
  307. screen.blit(image, img_rect)
  308. screen.blit(date, date_rect)
  309.  
  310. # Day 5
  311. date = small_font.render(days[4], True, black, white)
  312. date_rect = date.get_rect()
  313. date_rect.centerx = 500
  314. date_rect.top = 410
  315.  
  316. high = small_font.render('high: ', True, black, white)
  317. high_rect = high.get_rect()
  318. high_temp = comfortaa_small.render(highs[4], True, black, white)
  319. htemp_rect = high_temp.get_rect()
  320. high_rect.topright = (500, 630)
  321. htemp_rect.midleft = high_rect.midright
  322.  
  323. low = small_font.render("low: ", True, black, white)
  324. low_rect = low.get_rect()
  325. low_rect.topright = (500, 710)
  326. low_temp = comfortaa_small.render(lows[4], True, black, white)
  327. ltemp_rect = low_temp.get_rect()
  328. ltemp_rect.midleft = low_rect.midright
  329.  
  330.  
  331. condition = tiny_font.render(conditions[4], True, black, white)
  332. con_rect = condition.get_rect()
  333. con_rect.centerx = 500
  334. con_rect.top = 450
  335.  
  336. image = pygame.image.load(img_links[4])
  337. image.set_colorkey((255, 255, 255))
  338. img_rect = image.get_rect()
  339. img_rect.center = (500, 540)
  340.  
  341. screen.blit(condition, con_rect)
  342. screen.blit(high, high_rect)
  343. screen.blit(degrees, htemp_rect.topright)
  344. screen.blit(degrees, ltemp_rect.topright)
  345. screen.blit(high_temp, htemp_rect)
  346. screen.blit(low, low_rect)
  347. screen.blit(low_temp, ltemp_rect)
  348. screen.blit(image, img_rect)
  349. screen.blit(date, date_rect)
  350.  
  351. update_time = "Last updated at " + datetime.now().strftime("%l:%M%P")
  352. last_update = tiny_font.render(update_time, True, gray, white)
  353. screen.blit(last_update, (5, 770))
  354.  
  355. #pygame.display.update()
  356. #call(["/mnt/onboard/.python/eink_update.sh"])
  357. convert_to_raw(screen)
  358. # call(["/mnt/onboard/.python/display_raw.sh"])
  359. print 'done'
  360.  
  361. #try:
  362. get_weather_data()
  363. #except IndexError as error:
  364. #index_error(error)
  365.  
  366. # You will need the koboroot.tgz mentioned on the other paste
  367. # It has the icons. For testing you'll need to run things from that directory. I think I hacked
  368. # the paths right but eh. Some of this does not work but it generates a jpeg.
  369. # I've stripped out the need for the location field and used a zip code. You will need to got to
  370. # World weather online and explore the API and get a free license key
  371. # If you want to do lat/long you'll have to explore the API
  372. # The link in the koboroot.tgz file is completely bogus. The key also did not work
  373. # I know I've forgotten something.
Add Comment
Please, Sign In to add comment