Last active
August 7, 2017 03:38
-
-
Save toracle/c02e5bb6410b7c2cd707eebfa221aa0b to your computer and use it in GitHub Desktop.
moviefriend-code-2
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 datetime import datetime | |
| from urllib.request import urlopen | |
| from urllib.parse import urlencode | |
| class LotteCinema(object): | |
| # base_url = 'http://www.lottecinema.co.kr' | |
| base_url = 'http://moviefriend.cafe24.com' | |
| base_url_cinema_data = '{}/LCWS/Cinema/CinemaData.aspx'.format(base_url) | |
| base_url_movie_list = '{}/LCWS/Ticketing/TicketingData.aspx'.format(base_url) | |
| def make_payload(self, **kwargs): | |
| param_list = {'channelType': 'MW', 'osType': '', 'osVersion': '', **kwargs} | |
| data = {'ParamList': json.dumps(param_list)} | |
| payload = urlencode(data).encode('utf8') | |
| return payload | |
| def byte_to_json(self, fp): | |
| content = fp.read().decode('utf8') | |
| return json.loads(content) | |
| def get_theater_list(self): | |
| url = self.base_url_cinema_data | |
| payload = self.make_payload(MethodName='GetCinemaItems') | |
| with urlopen(url, data=payload) as fin: | |
| json_content = self.byte_to_json(fin) | |
| return [ | |
| { | |
| 'TheaterName': '{} 롯데시네마'.format(entry.get('CinemaNameKR')), | |
| 'TheaterID': '{}|{}|{}'.format(entry.get('DivisionCode'), entry.get('SortSequence'), entry.get('CinemaID')), | |
| 'Longitude': entry.get('Longitude'), | |
| 'Latitude': entry.get('Latitude') | |
| } | |
| for entry in json_content.get('Cinemas').get('Items') | |
| ] | |
| def distance(self, x1, x2, y1, y2): | |
| dx = float(x1) - float(x2) | |
| dy = float(y1) - float(y2) | |
| distance = math.sqrt(dx**2 + dy**2) | |
| return distance | |
| def filter_nearest_theater(self, theater_list, pos_latitude, pos_longitude, n=3): | |
| distance_to_theater = [] | |
| for theater in theater_list: | |
| distance = self.distance(pos_latitude, theater.get('Latitude'), pos_longitude, theater.get('Longitude')) | |
| distance_to_theater.append((distance, theater)) | |
| return [theater for distance, theater in sorted(distance_to_theater, key=lambda x: x[0])[:n]] | |
| def get_movie_list(self, theater_id): | |
| url = self.base_url_movie_list | |
| target_dt = datetime.now() | |
| target_dt_str = target_dt.strftime('%Y-%m-%d') | |
| payload = self.make_payload(MethodName='GetPlaySequence', playDate=target_dt_str, cinemaID=theater_id, representationMovieCode='') | |
| with urlopen(url, data=payload) as fin: | |
| json_content = self.byte_to_json(fin) | |
| movie_id_to_info = {} | |
| for entry in json_content.get('PlaySeqsHeader', {}).get('Items', []): | |
| movie_id_to_info.setdefault(entry.get('MovieCode'), {})['Name'] = entry.get('MovieNameKR') | |
| for order, entry in enumerate(json_content.get('PlaySeqs').get('Items')): | |
| schedules = movie_id_to_info[entry.get('MovieCode')].setdefault('Schedules', []) | |
| schedule = { | |
| 'StartTime': '{}'.format(entry.get('StartTime')), | |
| 'RemainingSeat': int(entry.get('TotalSeatCount')) - int(entry.get('BookingSeatCount')) | |
| } | |
| schedules.append(schedule) | |
| return movie_id_to_info | |
| cinema = LotteCinema() | |
| print(cinema.filter_nearest_theater(cinema.get_theater_list(), 37.5, 126.844)) | |
| print(cinema.get_movie_list('1|2|1018')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment