Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
540
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.19 KB | None | 0 0
  1. import json, requests
  2. import MySQLdb
  3. import datetime
  4.  
  5. # Databases
  6. DEALFLICKS_DB_NAME = 'dealflicks'
  7. DEALFLICKS_DB_USER = 'root'
  8. DEALFLICKS_DB_PASSWORD = ''
  9. DEALFLICKS_DB_HOST = '127.0.0.1'
  10.  
  11.  
  12. VISTA_AUTH_BASE_URL = "https://staging-auth.moviexchange.com"
  13. VISTA_TRANSACT_BASE_URL = "http://staging-api-transact.moviexchange.com/reference-data/v1/"
  14.  
  15. #Vista Login Credentials
  16. VISTA_INTEGRATION_USERNAME = 'taylor@dealflicks.com'
  17. VISTA_INTEGRATION_PASSWORD = 'dealflix890'
  18. VISTA_CLIENT_ID = 'dealflicks'
  19.  
  20. db = MySQLdb.connect(host=DEALFLICKS_DB_HOST, db=DEALFLICKS_DB_NAME, user=DEALFLICKS_DB_USER, passwd=DEALFLICKS_DB_PASSWORD)
  21. dict_cursor = db.cursor(MySQLdb.cursors.DictCursor)
  22.  
  23. exhibitor_films = dict([])
  24.  
  25. def get_token():
  26.     payload = "username=%s&password=%s&grant_type=password&client_id=%s" % (VISTA_INTEGRATION_USERNAME, VISTA_INTEGRATION_PASSWORD, VISTA_CLIENT_ID)
  27.  
  28.     r1 = requests.post(VISTA_AUTH_BASE_URL + '/connect/token',
  29.         headers={"Content-Type":"application/x-www-form-urlencoded"},
  30.         data=payload)
  31.  
  32.     body = json.loads(r1.content)
  33.     token = body["access_token"]
  34.     return token
  35.  
  36. def get_request_headers():
  37.     token = get_token()
  38.     return {
  39.         "Authorization": "Bearer " + token,
  40.         "Content-Type": "application/json"
  41.     }
  42.  
  43. def get_cinema_chain_id():
  44.     res = requests.get(VISTA_TRANSACT_BASE_URL + 'cinema-chains', headers=get_request_headers())
  45.     resp = res.json()
  46.     for cinema_chain in resp:
  47.         yield cinema_chain['id'], cinema_chain['name']
  48.  
  49. def get_films_for_cinema_chain(cinema_chain_id):
  50.     get_films_url_path = 'cinema-chains/{cinema_chain_id}/films'.format(cinema_chain_id=cinema_chain_id)
  51.     get_films_full_url = VISTA_TRANSACT_BASE_URL + get_films_url_path
  52.  
  53.     res = requests.get(get_films_full_url, headers=get_request_headers())
  54.     resp = res.json()
  55.     dictionary = {'films': resp}
  56.     return dictionary
  57.  
  58. def get_sites_for_cinema_chain(cinema_chain_id, cinema_chain_name):
  59.     get_sites_url_path = 'cinema-chains/{cinema_chain_id}/sites'.format(cinema_chain_id=cinema_chain_id, cinema_chain_name=cinema_chain_name)
  60.     get_sites_full_url = VISTA_TRANSACT_BASE_URL + get_sites_url_path
  61.  
  62.     res = requests.get(get_sites_full_url, headers=get_request_headers())
  63.     resp = res.json()
  64.  
  65.     for site in resp:
  66.         print ("site:" + site['id'])
  67.         yield site['id'], site['name']
  68.  
  69. def get_showtimes_for_exhibitors():
  70.     for cinema_chain_id, cinema_chain_name in get_cinema_chain_id():
  71.         exhibitor_films = get_films_for_cinema_chain(cinema_chain_id)
  72.         get_showtime_for_cinema_chain(cinema_chain_id, cinema_chain_name)
  73.  
  74. def get_showtime_for_cinema_chain(chain_id, chain_name):
  75.     # get_venues_for_cinema_chain function here is just your get_venues function
  76.     # I rename it for readability
  77.     for site_id, site_name in get_sites_for_cinema_chain(chain_id, chain_name):
  78.         get_showtimes_for_site(site_id, site_name)
  79.  
  80. def get_showtimes_for_site(site_id, site_name):
  81.     get_showtimes_url_path = 'sites/{site_id}/showtimes'.format(site_id=site_id, site_name=site_name)
  82.     get_showtimes_for_site_url = VISTA_TRANSACT_BASE_URL + get_showtimes_url_path
  83.  
  84.     res = requests.get(get_showtimes_for_site_url, headers=get_request_headers())
  85.     resp = res.json()
  86.     dictionary = {u'showtimes':resp}
  87.    
  88.     for showtimeInfo in dictionary["showtimes"]:
  89.         process_showtime_for_site(site_id, site_name)
  90.  
  91.  
  92. def process_showtime_for_site(site_id, site_name):
  93.     for film in exhibitor_films.get("films"):
  94.         if film.get('id') == showtimesInfo.get("filmId"):
  95.             showtimesInfo['cinema_chain_name'] = cinema_chain_name
  96.             showtimesInfo['site_name'] = site_name
  97.             showtimesInfo['releaseDate'] = film['releaseDate']
  98.             showtimesInfo['title'] = film['title']
  99.             break
  100.  
  101.     # Now compare vista showtimesInfo with dealflicks database results
  102.     print(showtimesInfo)
  103.     dict_cursor.execute('''SELECT
  104.                                movie.name AS title,
  105.                                movie.theater_release_date AS releaseDate,
  106.                                theater.name AS site_name,
  107.                                theater_chain.name AS cinema_chain_name,
  108.                                showtime.datetime AS startTime,
  109.                                showtime.id AS showtime_id
  110.                                FROM showtime
  111.                                LEFT OUTER JOIN theater
  112.                                ON showtime.theater_id = theater.id
  113.                                LEFT OUTER JOIN pos_movie
  114.                                ON showtime.pos_movie_id = pos_movie.id
  115.                                LEFT OUTER JOIN movie
  116.                                ON pos_movie.movie_id = movie.id
  117.                                LEFT OUTER JOIN theater_chain
  118.                                ON theater.theater_chain_id=theater_chain.id                              
  119.                                WHERE movie.name = %s AND
  120.                                movie.theater_release_date = %s AND
  121.                                theater.name = %s AND
  122.                                theater_chain.name = %s AND
  123.                                showtime.datetime = %s''', (
  124.                                     showtimesInfo['title'],
  125.                                     showtimesInfo['releaseDate'].replace('T', ' ')[0:10],
  126.                                     showtimesInfo['site_name'],
  127.                                     showtimesInfo['cinema_chain_name'],
  128.                                     showtimesInfo['startTimeUtc'].replace('T', ' ')
  129.                         ))
  130.     results= dict_cursor.fetchall()
  131.  
  132.     for result in results:
  133.         if result:
  134.             print('data is already available | updating vista_session_id {} for showtime_id : {}'.format(showtimesInfo['id'], result.get('showtime_id')))
  135.  
  136.             query = '''UPDATE showtime SET vista_session_id = %s WHERE id = %s'''
  137.             dict_cursor.execute(query, [showtimesInfo['id'], result['showtime_id']])
  138.             db.commit()
  139.             print('database updated')
  140.             break
  141.     else:
  142.         print("No match found")
  143.  
  144. if __name__ == '__main__':
  145.     get_showtimes_for_exhibitors()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement