-
Notifications
You must be signed in to change notification settings - Fork 2
/
get_livestreams_and_names.py
60 lines (56 loc) · 1.82 KB
/
get_livestreams_and_names.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
import os
import csv
import re
import json
import StringIO
import codecs
import requests
import urllib
def get_cached_page(url):
name = os.path.join(
os.path.dirname(__file__),
"cache",
url.split("://")[1].replace("/", "---")
)
if not os.path.exists(name):
content = requests.get(url).content
try:
os.makedirs(os.path.dirname(name))
except OSError:
pass
with codecs.open(name, 'w', 'utf-8') as fh:
fh.write(content)
with codecs.open(name, 'r', 'utf-8') as fh:
content = fh.read()
return content
reader = csv.reader(open("Hurricane Sandy livestreams - Working cameras.csv"))
sources = []
items = iter(reader)
items.next()
items.next()
for url, locname, livethumb, embed_code, notes in items:
source = {
'location': locname,
'url': url,
'livethumb': livethumb,
'embed_code': embed_code,
}
if "ustream" in url:
source['provider'] = "ustream"
elif "livestream" in url:
source['provider'] = "livestream"
source['id'] = re.search("livestream.com/([^/]+)(/|$)", url).group(1)
elif "justin" in url:
source['provider'] = "justintv"
source['id'] = re.search("justin.tv/([^/#]+)(/|$|#)", url).group(1)
elif "earthcam" in url:
source['provider'] = "earthcam"
else:
source['provider'] = "other"
latlng = json.loads(get_cached_page(
"https://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=%s" % urllib.quote_plus(locname.encode('utf8'))
))
assert latlng['status'] != 'ZERO_RESULTS', "Geocode not found for %s" % locname
source['point'] = latlng['results'][0]['geometry']['location']
sources.append(source)
print "var data = %s" % json.dumps({'sources': sources}, indent=2)