-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoviextractor.py
103 lines (90 loc) · 3.08 KB
/
moviextractor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import sqlite3
conn = sqlite3.connect('movielens.sqlite')
cur = conn.cursor()
genre = input('Enter the category of movie: ')
genre = '%' + genre + '%'
tag = input('Enter the tag : ')
tag = '%' + tag + '%'
movie = {}
# this function calculate ratings of a movie by all users and returns average rating
def rating_chker(mid):
rating_list = []
cur.execute('SELECT ratings FROM Ratings WHERE m_id = ?', (mid,))
row = cur.fetchall()
for line in row:
rating = line[0]
rating_list.append(rating)
# if movie is not rated by any user
try:
add = sum(rating_list)
avg_rating = add/len(rating_list)
return avg_rating
except ZeroDivisionError:
return None
# it returns ctegory of movie
def category_chkr(mid):
cur.execute('SELECT category_id FROM Movies WHERE m_id = ?',(mid, ))
row = cur.fetchone()[0]
cur.execute('SELECT category FROM Category WHERE id = ?', (row, ))
category = cur.fetchone()[0]
return category
# it returns tag of movie if any
def tag_chkr(mid):
cur.execute('SELECT tag_id FROM Tags WHERE m_id = ?', (mid, ))
try:
row = cur.fetchone()[0]
cur.execute('SELECT tag FROM Tag WHERE id = ?', (row, ))
tag = cur.fetchone()[0]
return tag
except:
return None
cur.execute('SELECT id FROM Category WHERE category LIKE ? ', (genre, ))
row = cur.fetchall()
if len(row) < 1:
print('invalid category')
print('Now Searching according to Tags.....')
else:
for line in row:
catg = line[0]
cur.execute('SELECT m_id, m_name FROM Movies WHERE category_id = ?', (catg, ))
films = cur.fetchall()
for lst in films:
ids = lst[0]
name = lst[1]
movie[name] = ids
if len(tag) > 1:
cur.execute('SELECT id FROM Tag WHERE tag LIKE ? ', (tag, ))
row = cur.fetchall()
if len(row) < 1:
print('Could not find the tag')
else:
for line in row:
ids = line[0]
cur.execute('SELECT m_id FROM Tags WHERE tag_id = ?', (ids, ))
films = cur.fetchall()
for lst in films:
mid = lst[0]
if mid not in movie.values():
cur.execute('SELECT m_name FROM Movies WHERE m_id = ?', (mid,))
new_film = cur.fetchone()[0]
movie[new_film] = mid
else:
continue
else:
print("Wrong Tag")
if len(movie) < 1:
print('Movies not found ')
else:
print('According to filters ur movies are ')
try:
for key, value in movie.items():
rating = rating_chker(value)
category = category_chkr(value)
tags = tag_chkr(value)
try:
print('Movie: ', key, 'Avarage user Rating: {0:.2f}'.format(rating), 'Category: ', category, "Tag: ", tags)
except:
print('Movie: ', key, 'Movie is not rated by any user', 'Category: ', category, "Tag: ", tags)
except KeyboardInterrupt:
print('===========================')
print('Program intreupted by user')