Skip to content

Instantly share code, notes, and snippets.

@lucahammer
Last active April 16, 2022 20:14
Show Gist options
  • Save lucahammer/086ecd3d3ef7d9ac33586d33b061c00a to your computer and use it in GitHub Desktop.
Save lucahammer/086ecd3d3ef7d9ac33586d33b061c00a to your computer and use it in GitHub Desktop.
'''
MIT License
Copyright (c) 2022 Luca Hammer
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
'''
from TwitterAPI import TwitterAPI
import time
import requests
import pandas as pd
import matplotlib.pyplot as plt
plt.style.use('ggplot')
def get_and_save_history(today):
apikey = 'xxx' #change this
systemid = 123 #change this
payload = {
# https://pvoutput.org/help/api_specification.html#id15
'key': apikey,
'sid': systemid,
'd' : today,
'h' : 1, #get history
'limit' : 288
}
r = requests.get('https://pvoutput.org/service/r2/getstatus.jsp', params=payload)
with open(f'{today}-pvoutput.csv', 'w') as f:
for line in r.text.split(';'):
status = ','.join(line.split(',')[1:10]) #remove voltage because it has a comma and makes things broken
f.write(status)
f.write('\n')
def create_plot(today):
df = pd.read_csv(f'{today}-pvoutput.csv',
sep=',',
names=['Energy','Efficiency','Power','Average','Normalised','bla','blo','Temperature'])
p = df[['Power']].sort_index()['05:00':'21:00'].plot.area(
figsize=(10,5),
title=f'@HammerSolar generated {df["Energy"].max()/1000} kWh\n{time.strftime("%Y-%m-%d")}',
xlabel='Time',
ylabel='Watt',
color='#ffcd01',
alpha=0.95,
)
p.set_facecolor('#5f95fe')
plt.savefig('plot.png')
#create_plot(today)
def tweet(text, image_path):
api = TwitterAPI('xxx', #consumer key
'xxx', #consumer secret
'xxx', #oauth token
'xxx') #oauth token secret
# STEP 1 - upload image
file = open(image_path, 'rb')
data = file.read()
r = api.request('media/upload', None, {'media': data})
print('UPLOAD MEDIA SUCCESS' if r.status_code == 200 else 'UPLOAD MEDIA FAILURE: ' + r.text)
# STEP 2 - post tweet with a reference to uploaded image
if r.status_code == 200:
media_id = r.json()['media_id']
r = api.request('statuses/update', {'status': text, 'media_ids': media_id})
print('UPDATE STATUS SUCCESS' if r.status_code == 200 else 'UPDATE STATUS FAILURE: ' + r.text)
if __name__ == "__main__":
today = time.strftime("%Y%m%d")
get_and_save_history(today)
create_plot(today)
df = pd.read_csv(f'{today}-pvoutput.csv',
sep=',',
names=['Energy','Efficiency','Power','Average','Normalised','bla','blo','Temperature'])
text = f'Today I generated {df["Energy"].max()/1000} kWh of solar electricity. '+\
''+\
f'\n\nhttps://pvoutput.org/intraday.jsp?sid=xxx&dt={today}' #change xxx to your system id
tweet(text, 'plot.png')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment