-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
89 lines (79 loc) · 2.9 KB
/
main.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
import logging
import csv
import random
import datetime
import requests
from twython import Twython
import config
IMAGE_URL = 'https://maps.googleapis.com/maps/api/streetview'
METADATA_URL = 'https://maps.googleapis.com/maps/api/streetview/metadata'
TEMP_IMAGE_NAME = 'to_upload.jpeg'
def get_image(address, filename=TEMP_IMAGE_NAME, size='800x800'):
"""Get an image with some metadata
Parameters
----------
address : str
The address to pull
filename : str
Filename to save the file as
size : str, optional
Size of the image to request defaults to 800x800
"""
params = {
'size': size,
'location': address,
'key': config.API_KEY,
'fov': 70
}
metadata = requests.get(METADATA_URL, params=params)
if metadata.ok and metadata.json()['status'] == 'OK':
resp = requests.get(IMAGE_URL, params=params)
if resp.ok:
with open(filename, 'wb') as file_open:
file_open.write(resp.content)
return metadata.json()['date']
else:
raise Exception('Bad metadata? {}'.format(address))
def grab_random_picture():
"""Pull a random picture, save it and return the status"""
with open('big_list.txt') as file_open:
for rows, _ in enumerate(file_open.readlines()):
pass
with open('big_list.txt') as csvfile:
reader = csv.DictReader(csvfile, delimiter='|')
for i, record in zip(range(random.randint(0, rows)), reader):
pass
address_lookup = '{} {}, Toronto ON Canada'.format(record['address'], record['lfname'])
picture_date = get_image(address_lookup)
mun = record['mun_name']
if mun == 'former Toronto':
mun = 'Toronto'
status = '{} {}, {}'.format(
record['address'], record['lfname'], mun)
if len(record['name']) > 0:
status = record['name'] + ', ' + status
try:
date = datetime.datetime.strptime(picture_date, '%Y-%m')
status += ', {}'.format(date.strftime('%B %Y'))
except:
pass
return status
def post_image(status, image_file=TEMP_IMAGE_NAME):
"""Send a post with image"""
twitter = Twython(
config.APP_KEY, config.APP_SECRET, config.OAUTH_TOKEN, config.OAUTH_TOKEN_SECRET)
image = open(image_file, 'rb')
response = twitter.upload_media(media=image)
twitter.update_status(status=status, media_ids=[response['media_id']])
print('Posted!')
if __name__ == '__main__':
#for filename, address in [
# ('city.jpeg', '60 Queen St W, Toronto ON Canada'),
#('craven.jpeg', '709 Craven Road, Toronto ON Canada'),
#('yonge.jpeg', '5140 Yonge Street, Toronto ON Canada'),
#('robina.jpeg', '45 Robina Avenue, Toronto ON Canada')
#]:
# picture_date = get_image(address, filename)
# print('Address: {}, Date: {}'.format(address, picture_date))
status = grab_random_picture()
post_image(status)