Skip to content

Commit

Permalink
blizzard bot!!
Browse files Browse the repository at this point in the history
  • Loading branch information
lipeck committed Oct 31, 2021
1 parent 13f4742 commit 4cd72cd
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 4 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Two companion Twitter bots, [@snowinginithaca](https://twitter.com/snowinginitha

Snowing in Ithaca is a small passion project, begun summer 2014, when data was collected by looking out the window. After 2017, tweets were compiled by [IFTTT](https://ifttt.com/) triggers. As of December 2020, current weather data is sourced using the [OpenWeather API](https://openweathermap.org/api) and pushed with [Tweepy](https://www.tweepy.org/).

Snowed in Ithaca is a new bot that replies to its companion with the last year it snowed on a given date. Snowed in Ithaca calls on the [NOAA Web Services v2 API](https://www.ncdc.noaa.gov/cdo-web/webservices/v2#gettingStarted) alongside @crvaden's [NOAA API module](https://github.com/crvaden/NOAA_API_v2) and also tweets with Tweepy. This bot also uses [pytz](https://pypi.org/project/pytz/) and [dateutil](https://dateutil.readthedocs.io/en/stable/) to navigate timezones and dates.
Snowed in Ithaca was created in 2020 to reply to its companion with the last year it snowed on a given date and index NOAA history for the day's snow record. Snowed in Ithaca currently references a CSV containing NOAA data and tweets with Tweepy. This bot also uses [pytz](https://pypi.org/project/pytz/) and [dateutil](https://dateutil.readthedocs.io/en/stable/) to navigate timezones and dates.

NOAA data is sourced from [Cornell University's weather station](https://www.ncdc.noaa.gov/cdo-web/datasets/GHCND/stations/GHCND:USC00304174/detail), with local weather data from 1893-present. Snowed in Ithaca only replies if more than half an inch of snow was recorded. Both bots are hosted on [AWS Lambda](https://aws.amazon.com/).

Expand All @@ -16,8 +16,10 @@ These bots were built as part of coursework for Pratt School of Information INFO

* ow_twit.py
* this is the script that runs Snowing in Ithaca
* blizzard_bot.py
* this is the script that currently runs Snowed in Ithaca
* noaa_reply.py
* this is the script that runs Snowed in Ithaca
* former Snowed in Ithaca script
* test-and-setup
* this folder contains sections of code that were later combined into the two main scripts

Expand All @@ -29,10 +31,8 @@ Installing dependencies:
pip3 install tweepy
pip3 install pytz
pip3 install python-dateutil
git submodule add https://github.com/crvaden/NOAA_API_v2 noaa
```

API tokens required:
* Twitter API v1 token
* NOAA Web Services v2 API token
* OpenWeather API token
113 changes: 113 additions & 0 deletions blizzard_bot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import tweepy
import json
import requests
import datetime
import pytz #for timezones
from noaa.noaa_api_v2 import NOAAData
from dateutil.relativedelta import relativedelta
import time
import csv

with open('keys.json') as f:
keys = json.load(f)

consumer_key = keys[0]['twit_api']
consumer_secret = keys[0]['twit_api_secret']
access_token = keys[0]['sw_access']
access_token_secret = keys[0]['sw_access_secret']
api_token = keys[0]['noaa_api']

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

api = tweepy.API(auth)

#gets @snowinginithaca most recent tweet
status = api.user_timeline(id = 'snowinginithaca', count = 1)[0]

#tweepy parser with help from: https://towardsdatascience.com/tweepy-for-beginners-24baf21f2c25
json_str = json.dumps(status._json)
parsed = json.loads(json_str)
data = json.dumps(parsed)

#prints tweet in readable format
# print(json.dumps(parsed, indent = 4, sort_keys = True))

#pulling out data from tweet
tweet_id = parsed['id_str']
fav = parsed['favorited']
tweet_date = parsed['created_at']
retweet = parsed['retweeted']
reply = parsed['in_reply_to_screen_name']

# check if tweet is retweet or reply, terminate script if so
if retweet == True:
exit()
if reply != None:
exit()

# check if tweet has already been replied to, terminate script if so
if fav == True:
exit()

#reformats tweet date to ISO & changes timezone from UTC to eastern
date_iso = datetime.datetime.strptime(tweet_date, '%a %b %d %H:%M:%S %z %Y')
date_iso_est = date_iso.astimezone(pytz.timezone('America/New_York'))

#isolate date for NOAA
date_clean = date_iso_est.strftime('%Y-%m-%d')
date_csv = date_iso_est.strftime('-%m-%d')

# setting variables for loops
last, lastsnow, maxsnowfall, big_blizzard = 0, 0, 0, ''

# reads weather data on csv
with open('snowfall.csv', 'r') as f:
reader = csv.DictReader(f)

for row in reader:

# check dates & convert snowfall values to integers
date_col = row['DATE']
snow = int(float(row['SNOW']))

# checks for blizzard
if date_csv in date_col:
if snow > maxsnowfall:
maxsnowfall = snow
big_blizzard = date_col

else: continue

# checks for last snow
if date_csv in date_col and snow >= 0.5:

if last < int(date_col[:4]):
last = int(date_col[:4])
lastsnow = snow

else: continue

if last == 0: exit()

if last == int(big_blizzard[:4]):

# isolate year for tweet & separate out date info
blizzard = datetime.datetime.strptime(big_blizzard,'%Y-%m-%d')
blizzard_year = blizzard.strftime('%Y')
blizzard_day = blizzard.strftime('%B %-d')

# tweet & fav
api.update_status(f'@snowinginithaca the last time it snowed in Ithaca on {blizzard_day}, it snowed {lastsnow}" in {blizzard_year}! that\'s the most snow ever recorded in Ithaca on {blizzard_day}!', in_reply_to_status_id = tweet_id)
api.create_favorite(tweet_id)

if last != int(big_blizzard[:4]) and lastsnow > 0 and maxsnowfall > 0:

# isolate year for tweet & separate out date info
blizzard = datetime.datetime.strptime(big_blizzard,'%Y-%m-%d')
blizzard_year = blizzard.strftime('%Y')
blizzard_day = blizzard.strftime('%B %-d')

# tweet & fav
api.update_status(f'@snowinginithaca the last time it snowed in Ithaca on {blizzard_day}, it snowed {lastsnow}\" in {last}! the most snow ever recorded in Ithaca on {blizzard_day} is {maxsnowfall}\" in {blizzard_year}!', in_reply_to_status_id = tweet_id)
api.create_favorite(tweet_id)

0 comments on commit 4cd72cd

Please sign in to comment.