def check(film): db = HomeDB() is_present = db.check_entry(film) db.conn.close() if is_present is True: # Movie already present. return True return False
def get_similar(genre_list, comp_year): movie_list = [] db = HomeDB() cursors = [] my_dict = {} for item in genre_list: g1 = db.conn.execute( '''select movie_table.id, round(rating_table.score) from movie_table, rating_table, genre_table where movie_table.id = rating_table.id and movie_table.year > ? and movie_table.id = genre_table.id and genre_table.genre = ? collate nocase order by rating_table.score DESC''', (comp_year, item)).fetchall() l1 = list(g1) for item in l1: if item[0] not in my_dict: add_list = [1, item[1]] my_dict[item[0]] = add_list else: # print('Count = ' + str(my_dict[[item[0]]][0])) my_dict[item[0]][0] = my_dict[item[0]][0] + 1 # print(my_dict) sorted_dict = sorted(my_dict, key=my_dict.__getitem__, reverse=True) print('Sorted dictionary : ') print(type(sorted_dict)) return sorted_dict[:8]
def load_info(film): print('Loading film information.') db = HomeDB() row_id = db.get_row_id(film) if film.imdb_link is None: im = db.conn.execute( '''SELECT imdb_link from link_table where id = ?''', (row_id, )).fetchone()[0] film.imdb_link = im print('Loaded imdb link.') if film.rotten_link is None: # rot = db.conn.execute('''SELECT rotten_link from links_table where name =''') rot = db.conn.execute( '''SELECT rotten_link from link_table where id = ?''', (row_id, )).fetchone()[0] film.rotten_link = rot print('Loaded rt.') if film.score is None: film.score = db.conn.execute( '''SELECT ROUND(score) from rating_table WHERE id = ?''', (row_id, )).fetchone()[0] print('Loaded score.') if len(film.ratings) < 4: rating_row = db.conn.execute( '''SELECT imdb, meta, rotten, audience from rating_table where id = ?''', (row_id, )).fetchone() film.ratings['imdb'] = rating_row[0] film.ratings['meta'] = rating_row[1] film.ratings['rt'] = rating_row[2] film.ratings['audience'] = rating_row[3] print('Loaded ratings..') if len(film.genre) == 0: genres = db.conn.execute( '''SELECT genre from genre_table where id = ?''', (row_id, )).fetchall() for tup in genres: film.genre.append(tup[0]) print('Loaded genres.') if film.director is None: film.director = db.conn.execute( '''SELECT director from director_table where id = ?''', (row_id, )).fetchone()[0] print('Loaded director.') db.conn.close()
def check_movie(film=None, name=None, year=None): # name = film.name # year = film.year if name is not None: f = Film() f.name = name f.year = year check_movie(film=f) else: print('Validating movie information..') if check(film) is not True: print('Movie Not present in database. Adding..') print(film.name + ', ' + str(film.year)) if film.imdb_link is None: review_info1.load_imdb_url(film) review_info1.imdb_content(film) print(film.genre) review_info1.get_rotten_link(film) review_info1.rotten(film) review_info1.get_score(film) add_new(film) print('Added.') return else: db = HomeDB() row_id = db.get_row_id(film) # These snippets could be written as separate functions in order to improve functionality. # Checking for links if db.check_links(film) is None: print('Links not present in the database. Adding.') if film.imdb_link is None: review_info1.load_imdb_url(film) review_info1.get_rotten_link(film) db.add_links(film, row_id) db.conn.commit() print('Added new links for the movie.') else: print('Links present in the database.') # Checking for director if db.check_director(film) is None: print('Director not present. Adding.. ') review_info1.get_director(film) db.add_director(film, row_id) db.conn.commit() db.conn.close() load_info(film) print('Done verifying..') return
def home(): db = HomeDB() popular_list = [] count = 0 movies = db.conn.execute('''SELECT * from popular_now''').fetchall() for item in movies: movie = [item[0], item[1], item[2]] # print(movie) posters = db.conn.execute( '''SELECT * from poster_table where id = ?''', (movie[0], )).fetchone() if posters is not None: poster = posters[1] else: poster = None # print(poster) movie.append(poster) popular_list.append(movie) count = count + 1 if count == 3: break print(popular_list) # db.conn.close() top_movies = [] count = 0 mov = db.conn.execute( '''select movie_table.name, movie_table.year, poster_table.poster_url from movie_table, poster_table,rating_table where movie_table.id=poster_table.id and movie_table.id=rating_table.id order by rating_table.score DESC; ''' ).fetchall() for items in mov: movie = [items[0], items[1], items[2]] count = count + 1 top_movies.append(movie) if count == 3: break return render_template('home.html', popular_list=popular_list, top_movies=top_movies)
def genre_search(): error = None if request.method == 'POST' or 'genre_name' in request.args: # Get genre if 'genre_name' in request.args: genre = request.args['genre_name'] else: genre = request.form['genre'] db = HomeDB() m_list = db.conn.execute( '''SELECT movie_table.id, movie_table.name, movie_table.year, ROUND(rating_table.score) FROM MOVIE_TABLE, GENRE_TABLE, RATING_TABLE WHERE MOVIE_TABLE.id = GENRE_TABLE.id AND MOVIE_TABLE.id = RATING_TABLE.id AND Genre_table.genre = ? COLLATE NOCASE ORDER BY RATING_TABLE.score DESC''', (genre, )) return render_template('view_genre.html', genre=genre, m_list=m_list) return render_template('get_genre.html', error=error)
from database.home_database import HomeDB from Film.film import Film from Film.get_content import load_url, imdb_meta, rotten, get_score import sys db = HomeDB() db.initialize_tables() # choice = input('Press 1 to add an entry : ') # if int(choice) == 1: # film = Film() # name = input('Enter name : ') # year = input('Year : ') # film.name = name # film.year = year # load_url(film) # imdb_meta(film) # rotten(film) # get_score(film) # # db.add_entry_to_main(film) # db.set_ratings(film) # # db.view_movies() print('Welcome to film-guide!') print('''Menu : 1) Search for a movie. 2) View current popular movies. 3) Have a look at top movies. 4) Critic favourites. 5) View movies by genre.
return name, year # movie_name = input('Enter movie name : ') url = 'http://www.bbc.com/culture/story/20160819-the-21st-centurys-100-greatest-films' ua = UserAgent() headers = {'User-Agent': ua.chrome} r = requests.get(url, headers=headers) soup = BeautifulSoup( r.content, 'lxml', ) gl = soup.find('div', class_='body-content') db = HomeDB() count = 0 fp = open('logfile', 'w') film_list = gl.select('p:nth-of-type(7)')[0] # fp.close() # db.conn.close() # exit() # print(film_list) for br in film_list.find_all('br'): try: next_s = br.next_sibling count = count + 1 if count < 87: continue print('Count : ' + str(count)) if not (next_s and isinstance(next_s, NavigableString)):
from database.home_database import HomeDB db = HomeDB() genres = db.conn.execute( '''SELECT genre from genre_table where id = ?''', (10, )).fetchall() print(genres)
def poster(movie=None, year=None, id=None): db = HomeDB() check_url = db.conn.execute('''SELECT poster_url from poster_table where poster_table.id = ?''', (id,)).fetchone() if check_url is not None: print(check_url) db.conn.close() return check_url[0] headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36' } url = 'http://www.ipecho.net/plain' # r = requests.get(url, headers=headers) # print(r.text) # db = HomeDB() # max_id = db.conn.execute('SELECT MAX(id) from movie_table').fetchone()[0] # print(max_id) # m_id = random.randint(0,max_id) # row = None # while row is None: # row = db.conn.execute('SELECT name, year from movie_table where id = ?', (m_id,)).fetchone() # print(row) # db.conn.close() # exit() # movie = input('Enter movie : ') # year = input('Enter year : ') # movie = str(row[0]) # year = str(row[1]) query = movie + ' ' + year + ' wikipedia' params = { 'q': query, 'oq': query } url = 'http://www.google.com/search?' r = requests.get(url, headers=headers, params=params) print(r.url) # print(r.content) soup = BeautifulSoup(r.content, 'lxml') h3 = soup.find('h3', class_='r') a = h3.find('a') print(a) link = a['href'] r = requests.get(link, headers=headers) print(r.url) soup = BeautifulSoup(r.content, 'lxml') table = soup.find ('table', class_='infobox vevent') a = table.find('a', class_='image') ref = 'http://www.wikipedia.org' + a['href'] print(ref) r = requests.get(ref, headers=headers) soup = BeautifulSoup(r.content, 'lxml') div = soup.find('div', id='file') img = div.find('img') link = 'http:' + img['src'] print(link) db.conn.execute('''INSERT INTO poster_table values(?,?)''', (id, link)) db.conn.commit() db.conn.close() return link
def add_new(film): db = HomeDB() db.add_entry_to_main(film) db.set_ratings(film) row_id = db.get_row_id(film) db.add_director(film, row_id) db.add_links(film, row_id) db.conn.commit() db.conn.close()
from fake_useragent import UserAgent from Film.film import Film from Film.get_content import imdb_meta, rotten, get_score, load_url, add_details from database.home_database import HomeDB import sys import time url = 'http://www.imdb.com/chart/moviemeter' ua = UserAgent() headers = {'User-Agent': ua.chrome} r = requests.get(url, headers=headers) count = 0 soup = BeautifulSoup(r.content, 'lxml') tbody = soup.find('tbody', class_='lister-list') db = HomeDB() with open('../schema/popular.sql', 'r') as sq: sql = sq.read() db.conn.executescript(sql) # db.conn.close() # exit() # db.conn.close() # exit() for tr in tbody.find_all('tr'): try: if count > 20: break count = count + 1 film = Film()
def result(): # print(request.cookies) # print(session) params = session['params'] db = HomeDB() # print(params[0]) # print(params[1]) print(type(params)) print(params) movie = params['name'].strip() year = str(params['year']).strip() # movie = params[0].strip() # year = str(params[1]).strip() print(str(movie) + ', ' + str(year)) # scores = db.conn.execute('''SELECT round(rating_table.score) from rating_table''') # score = db.conn.execute('''SELECT round(rating_table.score) from rating_table, movie_table # where movie_table.id = rating_table.id # and movie_table.name = ? COLLATE NOCASE # and movie_table.year = ? COLLATE NOCASE''', (movie, year)).fetchone()[0] score = params['score'] print('Score : ' + str(score)) movie_id = db.conn.execute( '''SELECT movie_table.id from movie_table where movie_table.name = ? COLLATE NOCASE and movie_table.year = ? COLLATE NOCASE''', (movie, year)).fetchone()[0] comp_year = int(year) - 20 # Getting poster link = poster(params['name'], params['year'], movie_id) params['link'] = link # Getting recommendations temp_list = get_similar(params['genres'], comp_year) # temp_list = get_similar(params[6], comp_year) recom = [] for item in temp_list: if item != movie_id: tup = db.conn.execute( '''SELECT movie_table.name, movie_table.year, round(rating_table.score) from movie_table, rating_table where movie_table.id = rating_table.id AND movie_table.id = ?''', (item, )).fetchone() recom.append(tup) print(tup) print('List gathered : ') print(temp_list) del temp_list db.conn.close() # get_similar(params[6]) # params['score'] = score # params.append(score) if params['summary'] is None: review = 'No summary available' else: review = params['summary'] # if 'review' in session: # review = session['review'][:511] + '....' # else: # review = 'No summary available.' # return params[0] # print(review) return render_template('movie.html', params=params, review=review, recom=recom)
def show_movies(): db = HomeDB() m_list = db.conn.execute( 'select * from movie_table where id <= 20').fetchall() db.conn.close() return render_template('result.html', m_list=m_list)