-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoviedb.py
64 lines (47 loc) · 1.82 KB
/
moviedb.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
import sqlite3
import os
conn = sqlite3.connect('data/db/movie.db')
def _build_basic_query_where(slots):
"""
Build the "where" part of a query by looking at the available slots
"""
# mappings from slot name to column name in DB
mappings = {
"actor_name": "actors",
"country_name": "country",
"director_name": "director",
"movie_description": "plot_keywords",
"movie_genre": "genres",
"movie_gross_revenue": "gross",
"movie_language": "language",
"movie_location": "country",
"movie_name": "title",
"movie_release_date": "year",
"movie_release_region": "country",
"movie_star_rating": "imdb_score",
"movie_subject": "plot_keywords",
"person_name": "actors",
}
# replace every space with at least one character followed by
# multiple characters
def preprocess_slot_value(x): return x.replace(" ", "_%")
query = ""
# available slots
av_slots = [x for x in slots.keys() if x in mappings.keys()]
if len(av_slots) > 0:
for slt in av_slots:
query += f" {mappings[slt]} like " + \
f"\"%{preprocess_slot_value(slots[slt])}%\" and "
query = " where " + query[:-4] # remove the last "and"
return query
def make_search_on_slots(slots, column="*", extra_where="", order_by="year"):
c = conn.cursor()
basic_where = _build_basic_query_where(slots)
if basic_where == "" and extra_where != "":
basic_where = " where "
query = f"select {column} from movie {basic_where} {extra_where} order by {order_by}"
cursor = conn.execute(query)
data = [[str(itm).strip() if itm != "" else "I don't have this information in my database" for itm in row]
for row in cursor.fetchall()]
cursor.close()
return data