def __init__(self, league): self.league = league self.db = InteractWithMovieDb() self.baseURL = 'http://www.boxofficemojo.com/schedule/?view=&release=&date=%s-%s' \ '&showweeks=5&p=.htm' self.start_date = None self.end_date = None
def get_all_league_movies(self, league, json=False): db = InteractWithMovieDb() list_ = db.get_league_movie_list(league) if json: return list_, self.jsonify(list_) else: return list_
def __init__(self, league, user): self.league = league self.user = user self.db = InteractWithMovieDb() self.args = self.get_my_movies() self.foreign_gross = [] self.domestic_gross = [] self.worldwide_gross = []
def __init__(self, league_name, league_owner, start_date, end_date, end_record): self.league_name = league_name self.league_owner = league_owner self.start_date = start_date self.end_date = end_date self.end_record = end_record self.db = InteractWithMovieDb()
def my_movies_league_totals_info(user_info): my_movies = OrderedDict() db = InteractWithUsersDb() movie_db = InteractWithMovieDb() print user_info id = db.get_user_info(user_info["name"])[0][0] leagues = movie_db.get_users_leagues(str(id)) for league in leagues: league_info = movie_db.get_league_totals_for_all_users(league) my_movies[league] = league_info return my_movies
def add_user_movie_to_league_list(league, values): data = [] print values values = values.split(',') values = [values[idx:idx+2] for idx in range(0, len(values), 2)] for l in values: data.append("("+','.join(l)+")") data = ', '.join(data) print data db = InteractWithMovieDb() db.add_user_movie_to_league_list(league, data)
class GetMovieList(object): def __init__(self, league): self.league = league self.db = InteractWithMovieDb() self.baseURL = 'http://www.boxofficemojo.com/schedule/?view=&release=&date=%s-%s' \ '&showweeks=5&p=.htm' self.start_date = None self.end_date = None def get_movie_by_month(self, year, month): url = self.baseURL % (year, month) html = urllib2.urlopen(url) soup = BeautifulSoup(html, "html.parser") for a in soup.find_all('a', href=True)[3:]: if 'movies' in a['href']: movie_id = a['href'].split('=')[1] movie_id = urllib.urlencode({'': movie_id})[1:-4] try: movie = GetMovieData(str(movie_id)) movie.find_movie_info() movie.find_movie_gross() movie_title, foreign_gross, domestic_gross, worldwide_gross, release_date = movie.get_movie_data_strings() movie_title = Scripts.fix_unicode(movie_title) movie_title = Scripts.fix_acii(movie_title) if foreign_gross == 'n/a': foreign_gross = None if domestic_gross == 'n/a': domestic_gross = None if worldwide_gross == 'n/a': worldwide_gross = None if worldwide_gross is None: s = Scripts() _foreign = s.convert_dollar_to_int(foreign_gross) _domestic = s.convert_dollar_to_int(domestic_gross) worldwide_gross = sum([_foreign, _domestic]) worldwide_gross = s.convert_int_to_dollar(worldwide_gross) date = release_date.split("-") if self.start_date < datetime.date(int(date[0]), int(date[1]), int(date[2])) < self.end_date: if self.db.check_movie_exists(self.league, movie_id) == 0: print 'Adding %s to db' % movie_title self.db.add_movie_to_db(self.league, movie_title, movie_id, foreign_gross, domestic_gross, worldwide_gross, release_date) else: print 'Updating %s in db' % movie_title self.db.update_movie_gross(self.league, foreign_gross, domestic_gross, worldwide_gross, movie_id) else: print "Not in range, %s" % release_date except Exception, e: print e print movie_id
class GetMyLeagues(object): def __init__(self, user): self.user = user # users json self.db = InteractWithMovieDb() def get_users_leagues(self): my_leagues = [] leagues = self.db.get_user_leagues(self.user['name']) for league in leagues: my_leagues.append(self.db.get_league_names(league)) return my_leagues[0][0]
def add_user_to_league_by_token(user_info, token): db = InteractWithUsersDb() id = db.get_user_info(user_info["name"])[0][0] info = Scripts.token_load(token) movie_db = InteractWithMovieDb() users = movie_db.get_league_users_ids(info) for i in users: if id in i: return False movie_db.add_user_to_league_user(info, id) return True
def my_movies(user_info): my_movies = OrderedDict() db = InteractWithUsersDb() movie_db = InteractWithMovieDb() id = db.get_user_info(user_info["name"])[0][0] leagues = movie_db.get_users_leagues(str(id)) for league in leagues: movie_data = movie_db.get_users_movie_info_from_league(league, id) totals = GetMyMovies(league, id) totals.get_my_movie_gross() movie_data.append(totals.get_all_my_totals()) my_movies[league] = movie_data return my_movies
class GetMyMovies(object): def __init__(self, league, user): self.league = league self.user = user self.db = InteractWithMovieDb() self.args = self.get_my_movies() self.foreign_gross = [] self.domestic_gross = [] self.worldwide_gross = [] def get_my_movies(self): return self.db.get_users_movie_from_league(self.league, self.user) def get_my_movie_gross(self): for movie in self.args: movie_gross = self.db.get_movie_from_db(self.league, movie) self.foreign_gross.append(movie_gross[0][0]) self.domestic_gross.append(movie_gross[0][1]) self.worldwide_gross.append(movie_gross[0][2]) def get_my_total(self, list_): for amount in range(len(list_)): if list_[amount] in ['None', 'n/a', 'N/A']: list_[amount] = 0 else: list_[amount] = self.convert_dollar_to_int(list_[amount]) total = sum(list_) return self.convert_int_to_dollar(total) def get_my_foreign_total(self): return self.get_my_total(self.foreign_gross) def get_my_domestic_total(self): return self.get_my_total(self.domestic_gross) def get_my_worldwide_total(self): return self.get_my_total(self.worldwide_gross) def get_all_my_totals(self): ft = self.get_my_foreign_total() dt = self.get_my_domestic_total() wt = self.get_my_worldwide_total() return ["Total", "", ft, dt, wt] def convert_int_to_dollar(self, dollar): return '${:,}'.format(dollar) def convert_dollar_to_int(self, dollar): dollar = dollar.replace("$", "") dollar = dollar.replace(",", "") return int(dollar)
def get_full_league_stats(league): movie_db = InteractWithMovieDb() rtn_list = {} for i in movie_db.get_league_users_and_movies(league): lst = list(i) for a in lst: if isinstance(a, datetime.date): lst[lst.index(a)] = a.strftime('%Y-%m-%d') if lst[0] not in rtn_list: rtn_list[lst[0]] = [] rtn_list[lst[0]].append(lst[1:]) else: rtn_list[lst[0]].append(lst[1:]) return rtn_list
def get_date_range(league): db = InteractWithMovieDb() dates = db.get_league_info(league) start_date = dates[0][0] start_date_serial = start_date.isoformat() end_date = dates[0][1] end_date_serial = end_date.isoformat() dates_list = OrderedDict( ((start_date + timedelta(_)).strftime(r"%Y-%m"), None) for _ in xrange((end_date - start_date).days)).keys() if start_date.month != end_date.month and start_date.year != end_date.year: dates_list.append(end_date.strftime(r"%Y-%m")) for date in dates_list: year, month = date.split('-') get_movie_by_month(league, start_date_serial, end_date_serial, year, month)
def main_sub(league, movie): db = InteractWithMovieDb() movie_id = movie movie = GetMovieData(movie_id) movie_title, foreign_gross, domestic_gross, worldwide_gross, release_date = movie.get_update_info( ) if foreign_gross == 'n/a': foreign_gross = None if domestic_gross == 'n/a': domestic_gross = None if worldwide_gross == 'n/a': worldwide_gross = None if worldwide_gross is None: s = Scripts() _foreign = s.convert_dollar_to_int(foreign_gross) _domestic = s.convert_dollar_to_int(domestic_gross) worldwide_gross = sum([_foreign, _domestic]) worldwide_gross = s.convert_int_to_dollar(worldwide_gross) print 'Updating %s in %s' % (movie_title, league[0]) db.update_movie_gross(league[0], foreign_gross, domestic_gross, worldwide_gross, movie_id)
class CreateLeague(object): def __init__(self, league_name, league_owner, start_date, end_date, end_record): self.league_name = league_name self.league_owner = league_owner self.start_date = start_date self.end_date = end_date self.end_record = end_record self.db = InteractWithMovieDb() def create_league(self): self.db.add_league_to_leagues(self.league_name, self.league_owner, self.start_date, self.end_date, self.end_record) self.db.create_league_movie_table(self.league_name) self.db.create_league_table(self.league_name) def check_date(self): pass def check_league_name(self): pass def check_league_owner(self): pass
def my_leagues_rankings(user_info): my_movies = OrderedDict() db = InteractWithUsersDb() movie_db = InteractWithMovieDb() id = db.get_user_info(user_info["name"])[0][0] leagues = movie_db.get_users_leagues(str(id)) for league in leagues: winner = ['No Winner', 0] me = [user_info["name"], 0] league_info = movie_db.get_league_totals_for_all_users(league) for i in league_info: entry = Scripts.convert_dollar_to_int(league_info[i]) if entry > winner[1]: winner = [i, entry] if i == user_info["name"]: me = [i, entry] if me == winner: me[1] = Scripts.convert_int_to_dollar(me[1]) my_movies[league] = [me] elif me != winner: winner[1] = Scripts.convert_int_to_dollar(winner[1]) me[1] = Scripts.convert_int_to_dollar(me[1]) my_movies[league] = [winner, me] return my_movies
def main(): db = InteractWithMovieDb() leagues = db.get_leagues_to_update() for league in leagues: for movie in db.get_movies_to_update(league[0]): main_sub(league, movie[0])
def __init__(self, user): self.user = user # users json self.db = InteractWithMovieDb()
def get_leagues(user): db = InteractWithMovieDb() udb = InteractWithUsersDb() uid = udb.get_user_info(user)[0][0] return db.get_users_leagues(uid)
def create_league(league_name, league_owner, start_date, end_date, end_record): print 'hi' db = InteractWithMovieDb() udb = InteractWithUsersDb() uid = udb.get_user_info(league_owner)[0][0] print 'hi1' league_name = league_name.replace(" ", "_") print 'hi2' print league_name, uid, str(start_date), str(end_date), str(end_record) db.add_league_to_leagues(league_name, uid, str(start_date), str(end_date), str(end_record)) print 'hi3' db.create_league_movie_table(league_name) print 'hi4' db.create_league_table(league_name) print 'hi5' db.create_league_user_table(league_name) print 'hi6' db.add_user_to_league_user(league_name, int(uid)) print 'hi7' # populate league_movies #gml = GetMovieList(league_name) #gml.get_date_range() tasks.get_date_range(league_name) print 'hi'
def get_all_movies_for_league(self, league): movie_db = InteractWithMovieDb() movies = movie_db.get_all_movies_for_league(league) print movies return movies