Created
July 27, 2017 14:11
-
-
Save toracle/b90ba5a7cbea81123d70bdf0b5ce6b1d to your computer and use it in GitHub Desktop.
moviefriend-code-1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import json | |
| import math | |
| from urllib.request import urlopen | |
| from urllib.parse import urlencode | |
| from datetime import datetime | |
| from datetime import timedelta | |
| class BoxOffice(object): | |
| base_url = 'http://www.kobis.or.kr/kobisopenapi/webservice/rest/boxoffice/'\ | |
| 'searchDailyBoxOfficeList.json' | |
| def __init__(self, api_key): | |
| self.api_key = api_key | |
| def get_movies(self): | |
| target_dt = datetime.now() - timedelta(days=1) | |
| target_dt_str = target_dt.strftime('%Y%m%d') | |
| query_url = '{}?key={}&targetDt={}'.format(self.base_url, self.api_key, target_dt_str) | |
| with urlopen(query_url) as fin: | |
| return json.loads(fin.read().decode('utf-8')) | |
| def simplify(self, result): | |
| return [ | |
| { | |
| 'rank': entry.get('rank'), | |
| 'name': entry.get('movieNm'), | |
| 'code': entry.get('movieCd') | |
| } | |
| for entry in result.get('boxOfficeResult').get('dailyBoxOfficeList') | |
| ] | |
| box = BoxOffice(api_key) | |
| movies = box.get_movies() | |
| print(box.simplify(movies)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment