-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch_data.py
43 lines (31 loc) · 892 Bytes
/
fetch_data.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
import re
import sys
import requests
import json
from contextlib import closing
def request_imdb(imdb_id):
url = 'http://www.omdbapi.com/?i={}&plot=short&r=json'.format(imdb_id)
r = requests.get(url)
return r.json()
def top_250_ids():
with closing(open('top250.txt')) as f:
return [(i+1, line.rstrip('\r\n')) for i, line in enumerate(f)]
def fetch_remote():
ids = top_250_ids()
movies = [(i, request_imdb(id)) for i, id in ids]
return movies
def save_to_cache(data):
with open('data.json', 'w') as f:
json.dump(data, f)
def fetch_local():
with open('data.json', 'r') as f:
data = json.load(f)
return data
def fetch_movies():
try:
data = fetch_local()
except IOError as e:
print("fetching data from remote api...")
data = fetch_remote()
save_to_cache(data)
return data