-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathweather.py
131 lines (89 loc) · 4.89 KB
/
weather.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# -*- coding: utf-8 -*-
"""
Created on Fri Dec 16 15:32:28 2016
@author: kilicm
"""
import json
import requests
import os
import tweepy
import time
# == OAuth Authentication ==
#
# This mode of authentication is the new preferred way
# of authenticating with Twitter.
# The consumer keys can be found on your application's Details
# page located at https://dev.twitter.com/apps (under "OAuth settings")
## The access tokens can be found on your applications's Details
## page located at https://dev.twitter.com/apps (located
## under "Your access token")
with open('mycreds.json') as json_data:
my_credentials = json.load(json_data)
consumer_key = my_credentials['mycreds']['twitterapi']['consumer_key']
consumer_secret = my_credentials['mycreds']['twitterapi']['consumer_secret']
access_token = my_credentials['mycreds']['twitterapi']['access_token']
access_token_secret = my_credentials['mycreds']['twitterapi']['access_token_secret']
weather_api = my_credentials['mycreds']['weatherapi']['apiid']
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
# If the authentication was successful, you should
# see the name of the account print out
print(api.me().name)
# If the application settings are set for "Read and Write" then
# this line should tweet out the message to your account's
# timeline. The "Read and Write" setting is on https://dev.twitter.com/apps
def get_weather_json(city):
req = requests.get("http://api.openweathermap.org/data/2.5/weather?q={}&units=imperial&appid={}".format(city, weather_api))
return req.json()
def get_weather_icon():
description = get_weather_json(city)['weather'][0]['description']
fn = os.path.abspath("/Users/kilicm/Desktop/Python Examples/icons")
icon = ''
if description == 'clear sky':
icon = fn + '/01d.png'
elif description == 'few clouds':
icon = fn + '/02d.png'
elif description == 'scattered clouds':
icon = fn + '/03d.png'
elif description in ['broken clouds','overcast clouds']:
icon = fn + '/04d.png'
elif description in ['light intensity drizzle','drizzle','heavy intensity drizzle','light intensity drizzle rain','drizzle rain','heavy intensity drizzle rain','shower rain and drizzle','heavy shower rain and drizzle','shower drizzle','light intensity shower rain','shower rain',' heavy intensity shower rain','ragged shower rain']:
icon = fn + '/09d.png'
elif description in ['light rain','moderate rain','heavy intensity rain','very heavy rain', 'extreme rain']:
icon = fn + '/10d.png'
elif description in ['thunderstorm with light rain','thunderstorm with rain','thunderstorm with heavy rain','light thunderstorm','thunderstorm','heavy thunderstorm','ragged thunderstorm','thunderstorm with light drizzle','thunderstorm with drizzle','thunderstorm with heavy drizzle']:
icon = fn + '/11d.png'
elif description in ['light snow','snow','heavy snow','sleet','shower sleet','light rain and snow','rain and snow','light shower snow','shower snow','heavy shower snow','freezing rain']:
icon = fn + '/13d.png'
elif description in ['mist','smoke','haze',"sand, dust whirls",'fog','sand','dust','volcanic ash','squalls','tornado']:
icon = fn + '/50d.png'
return icon
def get_weather_description():
return get_weather_json(city)['weather'][0]['description']
if __name__ == '__main__':
while True:
try:
tweets = api.search('weather today', geocode="39.8,-95.583068847656,2500km", count=10, lang="en")
except tweepy.RateLimitError:
time.sleep(6000*15)
continue
for tweet in tweets :
username = tweet.user.screen_name
user_id = tweet.id
text = tweet.text
city = tweet.user.location
print("User tweet is {}".format(text))
print("User name is @{}".format(username))
print("User city is {}".format(city))
print(".................................")
tbaglanti = "https://twitter.com/{}/status/{}".format(username, user_id)
try:
current_weather, temp_max, temp_min = [get_weather_json(city)['main'][i] for i in ['temp', 'temp_max', 'temp_min']]
except KeyError:
break
pri = ('{} @{} Weather: Currently {} & {:.0f}°F. Expect a high of {:.0f}°F and a low of {:.0f}°F.'.format(tbaglanti, username, get_weather_description(), current_weather,temp_max, temp_min))
print (pri)
api.update_with_media(get_weather_icon(), status=pri)
time.sleep(30000)
#