Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import json, requests
- import MySQLdb
- import datetime
- # Databases
- DEALFLICKS_DB_NAME = 'dealflicks'
- DEALFLICKS_DB_USER = 'root'
- DEALFLICKS_DB_PASSWORD = ''
- DEALFLICKS_DB_HOST = '127.0.0.1'
- VISTA_AUTH_BASE_URL = "https://staging-auth.moviexchange.com"
- VISTA_TRANSACT_BASE_URL = "http://staging-api-transact.moviexchange.com/reference-data/v1/"
- #Vista Login Credentials
- VISTA_INTEGRATION_USERNAME = 'taylor@dealflicks.com'
- VISTA_INTEGRATION_PASSWORD = 'dealflix890'
- VISTA_CLIENT_ID = 'dealflicks'
- db = MySQLdb.connect(host=DEALFLICKS_DB_HOST, db=DEALFLICKS_DB_NAME, user=DEALFLICKS_DB_USER, passwd=DEALFLICKS_DB_PASSWORD)
- dict_cursor = db.cursor(MySQLdb.cursors.DictCursor)
- exhibitor_films = dict([])
- def get_token():
- payload = "username=%s&password=%s&grant_type=password&client_id=%s" % (VISTA_INTEGRATION_USERNAME, VISTA_INTEGRATION_PASSWORD, VISTA_CLIENT_ID)
- r1 = requests.post(VISTA_AUTH_BASE_URL + '/connect/token',
- headers={"Content-Type":"application/x-www-form-urlencoded"},
- data=payload)
- body = json.loads(r1.content)
- token = body["access_token"]
- return token
- def get_request_headers():
- token = get_token()
- return {
- "Authorization": "Bearer " + token,
- "Content-Type": "application/json"
- }
- def get_cinema_chain_id():
- res = requests.get(VISTA_TRANSACT_BASE_URL + 'cinema-chains', headers=get_request_headers())
- resp = res.json()
- for cinema_chain in resp:
- yield cinema_chain['id'], cinema_chain['name']
- def get_films_for_cinema_chain(cinema_chain_id):
- get_films_url_path = 'cinema-chains/{cinema_chain_id}/films'.format(cinema_chain_id=cinema_chain_id)
- get_films_full_url = VISTA_TRANSACT_BASE_URL + get_films_url_path
- res = requests.get(get_films_full_url, headers=get_request_headers())
- resp = res.json()
- dictionary = {'films': resp}
- return dictionary
- def get_sites_for_cinema_chain(cinema_chain_id, cinema_chain_name):
- get_sites_url_path = 'cinema-chains/{cinema_chain_id}/sites'.format(cinema_chain_id=cinema_chain_id, cinema_chain_name=cinema_chain_name)
- get_sites_full_url = VISTA_TRANSACT_BASE_URL + get_sites_url_path
- res = requests.get(get_sites_full_url, headers=get_request_headers())
- resp = res.json()
- for site in resp:
- print ("site:" + site['id'])
- yield site['id'], site['name']
- def get_showtimes_for_exhibitors():
- for cinema_chain_id, cinema_chain_name in get_cinema_chain_id():
- exhibitor_films = get_films_for_cinema_chain(cinema_chain_id)
- get_showtime_for_cinema_chain(cinema_chain_id, cinema_chain_name)
- def get_showtime_for_cinema_chain(chain_id, chain_name):
- # get_venues_for_cinema_chain function here is just your get_venues function
- # I rename it for readability
- for site_id, site_name in get_sites_for_cinema_chain(chain_id, chain_name):
- get_showtimes_for_site(site_id, site_name)
- def get_showtimes_for_site(site_id, site_name):
- get_showtimes_url_path = 'sites/{site_id}/showtimes'.format(site_id=site_id, site_name=site_name)
- get_showtimes_for_site_url = VISTA_TRANSACT_BASE_URL + get_showtimes_url_path
- res = requests.get(get_showtimes_for_site_url, headers=get_request_headers())
- resp = res.json()
- dictionary = {u'showtimes':resp}
- for showtimeInfo in dictionary["showtimes"]:
- process_showtime_for_site(site_id, site_name)
- def process_showtime_for_site(site_id, site_name):
- for film in exhibitor_films.get("films"):
- if film.get('id') == showtimesInfo.get("filmId"):
- showtimesInfo['cinema_chain_name'] = cinema_chain_name
- showtimesInfo['site_name'] = site_name
- showtimesInfo['releaseDate'] = film['releaseDate']
- showtimesInfo['title'] = film['title']
- break
- # Now compare vista showtimesInfo with dealflicks database results
- print(showtimesInfo)
- dict_cursor.execute('''SELECT
- movie.name AS title,
- movie.theater_release_date AS releaseDate,
- theater.name AS site_name,
- theater_chain.name AS cinema_chain_name,
- showtime.datetime AS startTime,
- showtime.id AS showtime_id
- FROM showtime
- LEFT OUTER JOIN theater
- ON showtime.theater_id = theater.id
- LEFT OUTER JOIN pos_movie
- ON showtime.pos_movie_id = pos_movie.id
- LEFT OUTER JOIN movie
- ON pos_movie.movie_id = movie.id
- LEFT OUTER JOIN theater_chain
- ON theater.theater_chain_id=theater_chain.id
- WHERE movie.name = %s AND
- movie.theater_release_date = %s AND
- theater.name = %s AND
- theater_chain.name = %s AND
- showtime.datetime = %s''', (
- showtimesInfo['title'],
- showtimesInfo['releaseDate'].replace('T', ' ')[0:10],
- showtimesInfo['site_name'],
- showtimesInfo['cinema_chain_name'],
- showtimesInfo['startTimeUtc'].replace('T', ' ')
- ))
- results= dict_cursor.fetchall()
- for result in results:
- if result:
- print('data is already available | updating vista_session_id {} for showtime_id : {}'.format(showtimesInfo['id'], result.get('showtime_id')))
- query = '''UPDATE showtime SET vista_session_id = %s WHERE id = %s'''
- dict_cursor.execute(query, [showtimesInfo['id'], result['showtime_id']])
- db.commit()
- print('database updated')
- break
- else:
- print("No match found")
- if __name__ == '__main__':
- get_showtimes_for_exhibitors()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement