Advertisement
BoredOOMM

moreweather3.lua

Nov 7th, 2011
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.10 KB | None | 0 0
  1. --noaa weather mrpeachy nov 2011
  2. require 'cairo'
  3. require 'imlib2'
  4. ------------------------------------------------------------------------------
  5. function string:split(delimiter)
  6. local result = { }
  7. local from = 1
  8. local delim_from, delim_to = string.find( self, delimiter, from )
  9. while delim_from do
  10. table.insert( result, string.sub( self, from , delim_from-1 ) )
  11. from = delim_to + 1
  12. delim_from, delim_to = string.find( self, delimiter, from )
  13. end
  14. table.insert( result, string.sub( self, from ) )
  15. return result
  16. end
  17. --------------------------------------------------------------------------------
  18.  
  19. function conky_weather()
  20. if conky_window == nil then return end
  21. local cs = cairo_xlib_surface_create(conky_window.display, conky_window.drawable, conky_window.visual, conky_window.width, conky_window.height)
  22. cr = cairo_create(cs)
  23. local updates=tonumber(conky_parse('${updates}'))
  24. if updates>5 then
  25. --#########################################################################################################
  26. --SETUP--SETUP--SETUP--SETUP--SETUP--SETUP--SETUP--SETUP--SETUP--SETUP--SETUP
  27. secs=600--set update interval
  28. directory="/home/bkberger/wicon"--directory to store weather icons
  29. web="http://forecast.weather.gov/MapClick.php?lat=36.070955420413306&lon=-119.03626441955566"
  30. timer=(updates %secs)+1
  31. --get web address by going to weather.gov and entering your location in the box
  32. --#########################################################################################################
  33.  
  34. timer=(updates %secs)+1
  35. if timer==1 or updates==7 then
  36.  
  37. local f=io.popen("curl '"..web.."' | grep 'td width' | gawk -F'>' -v RS='<' 'RT{print $NF}'")
  38. data1=f:read("*a")
  39. data=string.split(data1,"\n")
  40. f:close()
  41. local f=io.popen("curl '"..web.."' | grep -A26 'Current Local Weather' | gawk -F'>' -v RS='<' 'RT{print $NF}' | sed -e 's/^[ \t]*//' -e '/^$/d'")
  42. data2=f:read("*a")
  43. current=string.split(data2,"\n")
  44. f:close()
  45. conditions=current[6]
  46. temperature=string.gsub(current[7]," &deg;","°").." "..string.gsub(current[8]," &deg;","°")
  47. humidity=current[11]
  48. windspeed=current[14]
  49. barometer=string.gsub(current[17],"&quot;","\"")
  50. dewpoint=string.gsub(current[20]," &deg;","°")
  51. windchill=string.gsub(current[23]," &deg;","°")
  52. --visibility=current[26]
  53. local f=io.popen("curl '"..web.."' | grep 'td width' | gawk -F'src=\"/images/wtf/' -v RS='\" width=' 'RT{print $NF}'")
  54. picloc1= f:read("*a")
  55. picloc=string.split(picloc1,"\n")
  56. f:close()
  57. --downloads images used by noaa-----------------------------------
  58. picnum=#picloc
  59. for i=1,picnum do
  60. os.execute("wget -nc --directory-prefix="..directory.." http://forecast.weather.gov/images/wtf/"..picloc[i].."")
  61. end
  62. end--timer section
  63.  
  64. start=3
  65. fday1a=data[start]
  66. fday1b=data[start+1]
  67. fc1a=data[start+5]
  68. fc1b=data[start+6]
  69. ft1=data[start+7]..string.gsub(data[start+8]," &deg;","°")
  70.  
  71. start=start+12
  72. fday2a=data[start]
  73. fday2b=data[start+1]
  74. fc2a=data[start+5]
  75. fc2b=data[start+6]
  76. ft2=data[start+7]..string.gsub(data[start+8]," &deg;","°")
  77.  
  78. start=start+12
  79. fday3a=data[start]
  80. fday3b=data[start+1]
  81. fc3a=data[start+5]
  82. fc3b=data[start+6]
  83. ft3=data[start+7]..string.gsub(data[start+8]," &deg;","°")
  84.  
  85. start=start+12
  86. fday4a=data[start]
  87. fday4b=data[start+1]
  88. fc4a=data[start+5]
  89. fc4b=data[start+6]
  90. ft4=data[start+7]..string.gsub(data[start+8]," &deg;","°")
  91.  
  92. start=start+12
  93. fday5a=data[start]
  94. fday5b=data[start+1]
  95. fc5a=data[start+5]
  96. fc5b=data[start+6]
  97. ft5=data[start+7]..string.gsub(data[start+8]," &deg;","°")
  98.  
  99. start=start+12
  100. fday6a=data[start]
  101. fday6b=data[start+1]
  102. fc6a=data[start+5]
  103. fc6b=data[start+6]
  104. ft6=data[start+7]..string.gsub(data[start+8]," &deg;","°")
  105.  
  106. start=start+12
  107. fday7a=data[start]
  108. fday7b=data[start+1]
  109. fc7a=data[start+5]
  110. fc7b=data[start+6]
  111. ft7=data[start+7]..string.gsub(data[start+8]," &deg;","°")
  112.  
  113. start=start+12
  114. fday8a=data[start]
  115. fday8b=data[start+1]
  116. fc8a=data[start+5]
  117. fc8b=data[start+6]
  118. ft8=data[start+7]..string.gsub(data[start+8]," &deg;","°")
  119.  
  120. start=start+12
  121. fday9a=data[start]
  122. fday9b=data[start+1]
  123. fc9a=data[start+5]
  124. fc9b=data[start+6]
  125. ft9=data[start+7]..string.gsub(data[start+8]," &deg;","°")
  126. --#####################################
  127. txpos1=30
  128. typos1=15
  129. --current conditions
  130. --text
  131. font="Mono"
  132. fsize=14
  133. xpos=txpos1
  134. ypos=typos1
  135. red,green,blue,alpha=1,1,1,1
  136. -------------------------------------
  137. text="Current Conditions"
  138. txt(text,xpos,ypos,font,14,red,green,blue,alpha)
  139. ypos=38
  140. fsize=10
  141. text="Conditions: "..conditions
  142. txt(text,xpos,ypos,font,fsize,red,green,blue,alpha)
  143. ypos=ypos+fsize+2
  144. text=" Temp: "..temperature
  145. txt(text,xpos,ypos,font,fsize,red,green,blue,alpha)
  146. ypos=ypos+fsize+2
  147. text=" Humidity: "..humidity
  148. txt(text,xpos,ypos,font,fsize,red,green,blue,alpha)
  149. ypos=ypos+fsize+2
  150. text="Wind Speed: "..windspeed
  151. txt(text,xpos,ypos,font,fsize,red,green,blue,alpha)
  152. ypos=ypos+fsize+2
  153. text=" Barometer: "..barometer
  154. txt(text,xpos,ypos,font,fsize,red,green,blue,alpha)
  155. ypos=ypos+fsize+2
  156. text=" Dewpoint: "..dewpoint
  157. txt(text,xpos,ypos,font,fsize,red,green,blue,alpha)
  158. ypos=ypos+fsize+2
  159. --text="Visibility: "..visibility
  160. --txt(text,xpos,ypos,font,fsize,red,green,blue,alpha)
  161. --forecast day1
  162. --image
  163. center_x=xpos+27+200
  164. center_y=24+35
  165. w=0
  166. h=0
  167. file=directory.."/"..picloc[1]
  168. image(center_x,center_y,w,h,file)
  169. --text
  170. font="Mono"
  171. fsize=10
  172. xpos=xpos+200
  173. ypos=typos1
  174. red,green,blue,alpha=1,1,1,1
  175. text=fday1a
  176. txt(text,xpos,ypos,font,fsize,red,green,blue,alpha)
  177. ypos=ypos+fsize+2
  178. text=fday1b
  179. txt(text,xpos,ypos,font,fsize,red,green,blue,alpha)
  180. ypos=ypos+72
  181. text=fc1a
  182. txt(text,xpos,ypos,font,fsize,red,green,blue,alpha)
  183. ypos=ypos+fsize+2
  184. text=fc1b
  185. txt(text,xpos,ypos,font,fsize,red,green,blue,alpha)
  186. ypos=ypos+fsize+2
  187. text=ft1
  188. txt(text,xpos,ypos,font,fsize,red,green,blue,alpha)
  189. --forecast day2
  190. --image
  191. center_x=center_x+70
  192. center_y=24+35
  193. w=0
  194. h=0
  195. file=directory.."/"..picloc[2]
  196. image(center_x,center_y,w,h,file)
  197. --text
  198. font="Mono"
  199. fsize=10
  200. xpos=xpos+70
  201. ypos=typos1
  202. red,green,blue,alpha=1,1,1,1
  203. text=fday2a
  204. txt(text,xpos,ypos,font,fsize,red,green,blue,alpha)
  205. ypos=ypos+fsize+2
  206. text=fday2b
  207. txt(text,xpos,ypos,font,fsize,red,green,blue,alpha)
  208. ypos=ypos+72
  209. text=fc2a
  210. txt(text,xpos,ypos,font,fsize,red,green,blue,alpha)
  211. ypos=ypos+fsize+2
  212. text=fc2b
  213. txt(text,xpos,ypos,font,fsize,red,green,blue,alpha)
  214. ypos=ypos+fsize+2
  215. text=ft2
  216. txt(text,xpos,ypos,font,fsize,red,green,blue,alpha)
  217. --forecast day3
  218. --image
  219. center_x=center_x+70
  220. center_y=24+35
  221. w=0
  222. h=0
  223. file=directory.."/"..picloc[3]
  224. image(center_x,center_y,w,h,file)
  225. --text
  226. font="Mono"
  227. fsize=10
  228. xpos=xpos+70
  229. ypos=typos1
  230. red,green,blue,alpha=1,1,1,1
  231. text=fday3a
  232. txt(text,xpos,ypos,font,fsize,red,green,blue,alpha)
  233. ypos=ypos+fsize+2
  234. text=fday3b
  235. txt(text,xpos,ypos,font,fsize,red,green,blue,alpha)
  236. ypos=ypos+72
  237. text=fc3a
  238. txt(text,xpos,ypos,font,fsize,red,green,blue,alpha)
  239. ypos=ypos+fsize+2
  240. text=fc3b
  241. txt(text,xpos,ypos,font,fsize,red,green,blue,alpha)
  242. ypos=ypos+fsize+2
  243. text=ft3
  244. txt(text,xpos,ypos,font,fsize,red,green,blue,alpha)
  245. --forecast day4
  246. --image
  247. center_x=center_x+70
  248. center_y=24+35
  249. w=0
  250. h=0
  251. file=directory.."/"..picloc[4]
  252. image(center_x,center_y,w,h,file)
  253. --text
  254. font="Mono"
  255. fsize=10
  256. xpos=xpos+70
  257. ypos=typos1
  258. red,green,blue,alpha=1,1,1,1
  259. text=fday4a
  260. txt(text,xpos,ypos,font,fsize,red,green,blue,alpha)
  261. ypos=ypos+fsize+2
  262. text=fday4b
  263. txt(text,xpos,ypos,font,fsize,red,green,blue,alpha)
  264. ypos=ypos+72
  265. text=fc4a
  266. txt(text,xpos,ypos,font,fsize,red,green,blue,alpha)
  267. ypos=ypos+fsize+2
  268. text=fc4b
  269. txt(text,xpos,ypos,font,fsize,red,green,blue,alpha)
  270. ypos=ypos+fsize+2
  271. text=ft4
  272. txt(text,xpos,ypos,font,fsize,red,green,blue,alpha)
  273. --forecast day5
  274. --image
  275. center_x=center_x+70
  276. center_y=24+35
  277. w=0
  278. h=0
  279. file=directory.."/"..picloc[5]
  280. image(center_x,center_y,w,h,file)
  281. --text
  282. font="Mono"
  283. fsize=10
  284. xpos=xpos+70
  285. ypos=typos1
  286. red,green,blue,alpha=1,1,1,1
  287. text=fday5a
  288. txt(text,xpos,ypos,font,fsize,red,green,blue,alpha)
  289. ypos=ypos+fsize+2
  290. text=fday5b
  291. txt(text,xpos,ypos,font,fsize,red,green,blue,alpha)
  292. ypos=ypos+72
  293. text=fc5a
  294. txt(text,xpos,ypos,font,fsize,red,green,blue,alpha)
  295. ypos=ypos+fsize+2
  296. text=fc5b
  297. txt(text,xpos,ypos,font,fsize,red,green,blue,alpha)
  298. ypos=ypos+fsize+2
  299. text=ft5
  300. txt(text,xpos,ypos,font,fsize,red,green,blue,alpha)
  301. --forecast day6
  302. --image
  303. center_x=center_x+70
  304. center_y=24+35
  305. w=0
  306. h=0
  307. file=directory.."/"..picloc[6]
  308. image(center_x,center_y,w,h,file)
  309. --text
  310. font="Mono"
  311. fsize=10
  312. xpos=xpos+70
  313. ypos=typos1
  314. red,green,blue,alpha=1,1,1,1
  315. text=fday6a
  316. txt(text,xpos,ypos,font,fsize,red,green,blue,alpha)
  317. ypos=ypos+fsize+2
  318. text=fday6b
  319. txt(text,xpos,ypos,font,fsize,red,green,blue,alpha)
  320. ypos=ypos+72
  321. text=fc6a
  322. txt(text,xpos,ypos,font,fsize,red,green,blue,alpha)
  323. ypos=ypos+fsize+2
  324. text=fc6b
  325. txt(text,xpos,ypos,font,fsize,red,green,blue,alpha)
  326. ypos=ypos+fsize+2
  327. text=ft6
  328. txt(text,xpos,ypos,font,fsize,red,green,blue,alpha)
  329. --forecast day7
  330. --image
  331. center_x=center_x+70
  332. center_y=24+35
  333. w=0
  334. h=0
  335. file=directory.."/"..picloc[7]
  336. image(center_x,center_y,w,h,file)
  337. --text
  338. font="Mono"
  339. fsize=10
  340. xpos=xpos+70
  341. ypos=typos1
  342. red,green,blue,alpha=1,1,1,1
  343. text=fday7a
  344. txt(text,xpos,ypos,font,fsize,red,green,blue,alpha)
  345. ypos=ypos+fsize+2
  346. text=fday7b
  347. txt(text,xpos,ypos,font,fsize,red,green,blue,alpha)
  348. ypos=ypos+72
  349. text=fc7a
  350. txt(text,xpos,ypos,font,fsize,red,green,blue,alpha)
  351. ypos=ypos+fsize+2
  352. text=fc7b
  353. txt(text,xpos,ypos,font,fsize,red,green,blue,alpha)
  354. ypos=ypos+fsize+2
  355. text=ft7
  356. txt(text,xpos,ypos,font,fsize,red,green,blue,alpha)
  357. --forecast day8
  358. --image
  359. center_x=center_x+70
  360. center_y=24+35
  361. w=0
  362. h=0
  363. file=directory.."/"..picloc[8]
  364. image(center_x,center_y,w,h,file)
  365. --text
  366. font="Mono"
  367. fsize=10
  368. xpos=xpos+70
  369. ypos=typos1
  370. red,green,blue,alpha=1,1,1,1
  371. text=fday8a
  372. txt(text,xpos,ypos,font,fsize,red,green,blue,alpha)
  373. ypos=ypos+fsize+2
  374. text=fday8b
  375. txt(text,xpos,ypos,font,fsize,red,green,blue,alpha)
  376. ypos=ypos+72
  377. text=fc8a
  378. txt(text,xpos,ypos,font,fsize,red,green,blue,alpha)
  379. ypos=ypos+fsize+2
  380. text=fc8b
  381. txt(text,xpos,ypos,font,fsize,red,green,blue,alpha)
  382. ypos=ypos+fsize+2
  383. text=ft8
  384. txt(text,xpos,ypos,font,fsize,red,green,blue,alpha)
  385. --forecast day9
  386. --image
  387. center_x=center_x+70
  388. center_y=24+35
  389. w=0
  390. h=0
  391. file=directory.."/"..picloc[9]
  392. image(center_x,center_y,w,h,file)
  393. --text
  394. font="Mono"
  395. fsize=10
  396. xpos=xpos+70
  397. ypos=typos1
  398. red,green,blue,alpha=1,1,1,1
  399. text=fday9a
  400. txt(text,xpos,ypos,font,fsize,red,green,blue,alpha)
  401. ypos=ypos+fsize+2
  402. text=fday9b
  403. txt(text,xpos,ypos,font,fsize,red,green,blue,alpha)
  404. ypos=ypos+72
  405. text=fc9a
  406. txt(text,xpos,ypos,font,fsize,red,green,blue,alpha)
  407. ypos=ypos+fsize+2
  408. text=fc9b
  409. txt(text,xpos,ypos,font,fsize,red,green,blue,alpha)
  410. ypos=ypos+fsize+2
  411. text=ft9
  412. txt(text,xpos,ypos,font,fsize,red,green,blue,alpha)
  413. --#########################################################################################################
  414. end-- if updates>5
  415.  
  416. cairo_destroy(cr)
  417. cairo_surface_destroy(cs)
  418. cr=nil
  419. return ""
  420. end-- end main function
  421.  
  422. function image(center_x,center_y,w,h,file)
  423. local show = imlib_load_image(file)
  424. if show == nil then return end
  425. imlib_context_set_image(show)
  426. if tonumber(w)==0 then
  427. width=imlib_image_get_width()
  428. else
  429. width=tonumber(w)
  430. end
  431. if tonumber(h)==0 then
  432. height=imlib_image_get_height()
  433. else
  434. height=tonumber(h)
  435. end
  436. local iacross=center_x-(width/2)
  437. local idown=center_y-(height/2)
  438. imlib_context_set_image(show)
  439. local scaled=imlib_create_cropped_scaled_image(0, 0, imlib_image_get_width(), imlib_image_get_height(), width, height)
  440. imlib_free_image()
  441. imlib_context_set_image(scaled)
  442. imlib_render_image_on_drawable(iacross, idown)
  443. imlib_free_image()
  444. show=nil
  445. end
  446. function txt(text,xpos,ypos,font,fsize,red,green,blue,alpha)
  447. cairo_select_font_face (cr, font, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
  448. cairo_set_font_size (cr, fsize)
  449. cairo_set_source_rgba (cr,red,green,blue,alpha)
  450. cairo_move_to (cr,xpos,ypos)
  451. cairo_show_text (cr,text)
  452. end--function
  453.  
  454. function rgb_to_r_g_b(colour,alpha)
  455. return ((colour / 0x10000) % 0x100) / 255., ((colour / 0x100) % 0x100) / 255., (colour % 0x100) / 255., alpha
  456. end
  457. function conky_draw_bg(r,x,y,w,h,color,alpha)
  458. if conky_window == nil then return end
  459. if cs == nil then cairo_surface_destroy(cs) end
  460. if cr == nil then cairo_destroy(cr) end
  461. local cs = cairo_xlib_surface_create(conky_window.display, conky_window.drawable, conky_window.visual, conky_window.width, conky_window.height)
  462. local cr = cairo_create(cs)
  463. cairo_set_source_rgba (cr,rgb_to_r_g_b(color,alpha))
  464. --top left mid circle
  465. local xtl=x+r
  466. local ytl=y+r
  467. --top right mid circle
  468. local xtr=(x+r)+((w)-(2*r))
  469. local ytr=y+r
  470. --bottom right mid circle
  471. local xbr=(x+r)+((w)-(2*r))
  472. local ybr=(y+r)+((h)-(2*r))
  473. --bottom right mid circle
  474. local xbl=(x+r)
  475. local ybl=(y+r)+((h)-(2*r))
  476. -----------------------------
  477. cairo_move_to (cr,xtl,ytl-r)
  478. cairo_line_to (cr,xtr,ytr-r)
  479. cairo_arc(cr,xtr,ytr,r,((2*math.pi/4)*3),((2*math.pi/4)*4))
  480. cairo_line_to (cr,xbr+r,ybr)
  481. cairo_arc(cr,xbr,ybr,r,((2*math.pi/4)*4),((2*math.pi/4)*1))
  482. cairo_line_to (cr,xbl,ybl+r)
  483. cairo_arc(cr,xbl,ybl,r,((2*math.pi/4)*1),((2*math.pi/4)*2))
  484. cairo_line_to (cr,xtl-r,ytl)
  485. cairo_arc(cr,xtl,ytl,r,((2*math.pi/4)*2),((2*math.pi/4)*3))
  486. cairo_close_path(cr)
  487. cairo_fill (cr)
  488. ------------------------------------------------------------
  489. cairo_surface_destroy(cs)
  490. cairo_destroy(cr)
  491. return ""
  492. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement