Skip to content

Instantly share code, notes, and snippets.

@alyoshenka
Last active January 24, 2021 03:44
Show Gist options
  • Save alyoshenka/d0d28092c3a0a46a782e7a4dfb16938e to your computer and use it in GitHub Desktop.
Save alyoshenka/d0d28092c3a0a46a782e7a4dfb16938e to your computer and use it in GitHub Desktop.
send me a text of the weather at a scheduled time
from sms import send
from weather import get_report
from weather import get_temperature
from weather import get_weather
from threading import Timer
import schedule
import time
import sys
# wait for network connection, else weird error
time.sleep(5)
report_times = { "07:00", "19:00" }
def go():
send(get_report())
return
def main():
args = sys.argv
if len(args) == 1: # no cli args
print("WeatherPi started")
print(get_weather())
print(get_temperature())
else: # if args
# quick n dirty solution
if args[1] == "weather":
print(get_weather())
if args[1] == "temperature":
print(get_temperature()):
return
# don't need to schedule currently
try:
for tm in report_times:
schedule.every().day.at(str(tm)).do(go)
while True:
schedule.run_pending()
time.sleep(1)
finally:
print("WeatherPi stopped")
return
if __name__ == "__main__":
main()
#!/usr/bin/python3
from sms import send
from weather import get_report
from weather import get_temperature
from weather import get_weather
from threading import Timer
import schedule
import time
import sys
# wait for network connection, else weird error
time.sleep(5)
report_times = { "07:00", "19:00" }
def go():
send(get_report())
return
def main():
args = sys.argv
if len(args) == 1: # no cli args
print("WeatherPi started")
print(get_weather())
print(get_temperature())
else: # if args
# quick n dirty solution
if args[1] == "weather":
print(get_weather())
if args[1] == "temperature":
print(get_temperature())
return
# don't need to schedule currently
try:
for tm in report_times:
schedule.every().day.at(str(tm)).do(go)
while True:
schedule.run_pending()
time.sleep(1)
finally:
print("WeatherPi stopped")
return
if __name__ == "__main__":
main()
#!/bin/bash
# install required python packages, run as root to allow run on startup
pip install twilio
pip install schedule
from twilio.rest import Client
def send(msg):
my_number = open("/home/jay/WeatherPi/MyNumber.txt").read()
sender_number = open("/home/jay/WeatherPi/TwilioNumber.txt").read()
sid = open("/home/jay/WeatherPi/TwilioSID.txt").read()
token = open("/home/jay/WeatherPi/TwilioToken.txt").read()
client = Client(sid, token)
client.messages.create(to=my_number, from_=sender_number, body=msg)
return
from requests import get
import json
from pprint import pprint
from datetime import datetime
def get_temperature():
url1 = 'http://api.openweathermap.org/data/2.5/weather?id='
url2 = '&units=imperial&APPID='
city_id = 5809844 # Seattle
apikey = open('/home/jay/WeatherPi/OpenWeatherMapAPIKey.txt').read().rstrip()
url = url1 + str(city_id) + url2 + str(apikey)
try:
local_weather = get(url).json()
ret = str(local_weather["main"]["temp"])
except:
ret = "-99"
return ret
def get_weather():
url1 = 'http://api.openweathermap.org/data/2.5/weather?id='
url2 = '&units=imperial&APPID='
city_id = 5809844 # Seattle
apikey = open('/home/jay/WeatherPi/OpenWeatherMapAPIKey.txt').read().rstrip()
url = url1 + str(city_id) + url2 + str(apikey)
try:
local_weather = get(url).json()
ret = str(local_weather["weather"][0]["description"])
except:
ret = "No connection"
return ret
def get_report():
url1 = 'http://api.openweathermap.org/data/2.5/weather?id='
url2 = '&units=imperial&APPID='
city_id = 5809844 # Seattle
apikey = open('/home/jay/WeatherPi/OpenWeatherMapAPIKey.txt').read().rstrip()
url = url1 + str(city_id) + url2 + str(apikey)
try:
local_weather = get(url).json()
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
print(local_weather["name"])
print(local_weather["weather"][0]["description"] )
ret = "The current time is " + current_time + "." \
+ " The weather in " + local_weather["name"] + " is " + local_weather["weather"][0]["description"] + "." \
+ " The temperature is " + str(local_weather["main"]["temp"]) + " degrees, but it feels like " + str(local_weather["main"]["feels_like"]) + " degrees."
except:
ret = "No connection"
print(ret)
return ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment