Advertisement
Guest User

locus_pv.py

a guest
Feb 20th, 2016
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. from __future__ import print_function
  2. import time
  3. import dryscrape
  4. import sys
  5. import subprocess
  6.  
  7. if 'linux' in sys.platform:
  8. # start xvfb in case no X is running. Make sure xvfb
  9. # is installed, otherwise this won't work!
  10. dryscrape.start_xvfb()
  11.  
  12. # This script downloads power and energy data from the Locus portal, saves to a local spreadsheet, and uploads to pvoutput.org
  13. # Run every 15 minutes from cron
  14.  
  15. username = 'login_email'
  16. password = 'password'
  17. PVOUTPUT_APIKEY = 'api_key'
  18. PVOUTPUT_SYSTEMID = 'numeric_systemID'
  19.  
  20. # setup a web scraping session
  21. sess = dryscrape.Session(base_url = 'https://solaros.datareadings.com/')
  22.  
  23. # we don't need images
  24. sess.set_attribute('auto_load_images', False)
  25.  
  26. # visit homepage and log in
  27. sess.visit('/')
  28.  
  29. username_field = sess.at_xpath('//*[@id="username"]')
  30. password_field = sess.at_xpath('//*[@id="password"]')
  31. btnlogin_field = sess.at_xpath('//*[@class="btn btn-primary"]')
  32.  
  33. username_field.set(username)
  34. password_field.set(password)
  35. btnlogin_field.click()
  36.  
  37. # Get values
  38. power = sess.at_xpath('//*[@name="instantValue"]',10).text()
  39. energy = sess.at_xpath('//*[@name="lifetimeValue"]',10).text()
  40.  
  41. if power == 'Offline':
  42. power = '0'
  43.  
  44. # print "Taking snapshot"
  45. # sess.render('website.png')
  46.  
  47. #Output values to a file
  48. f = open('spreadsheet.csv','a')
  49. print(time.strftime("%m/%d/%Y")+", "+time.strftime("%H:%M")+", "+power+", "+energy, file=f)
  50. f.close()
  51.  
  52. # Upload to pvoutput.org
  53. t_date = 'd={0}'.format(time.strftime('%Y%m%d'))
  54. t_time = 't={0}'.format(time.strftime('%H:%M'))
  55. t_energy = 'v1={0}'.format(1000*float(energy))
  56. t_power = 'v2={0}'.format(1000*float(power))
  57. cmd = ['/usr/bin/curl',
  58. '-d', t_date,
  59. '-d', t_time,
  60. '-d', t_energy,
  61. '-d', t_power,
  62. '-d', 'c1=1',
  63. '-H', 'X-Pvoutput-Apikey: ' + PVOUTPUT_APIKEY,
  64. '-H', 'X-Pvoutput-SystemId: ' + PVOUTPUT_SYSTEMID,
  65. 'http://pvoutput.org/service/r2/addstatus.jsp']
  66. # print(cmd)
  67. ret = subprocess.call (cmd)
  68. # print(ret)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement