-
Notifications
You must be signed in to change notification settings - Fork 5
/
fillingData.py
87 lines (60 loc) · 2.48 KB
/
fillingData.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
""" importing required modules """
from imdb import IMDb
from imdb import IMDbDataAccessError
from random import choice
import sqlite3
from string import digits
""" Asking the user the number of movies they want to add """
counter = int(input("Enter how many new movies you want?(e.g 10)"))
count = 0
""" Initialising the IMDB class and connecting to the local database """
movieman = IMDb()
db = sqlite3.connect("movieman.db")
mc = db.cursor()
""" Checks for no of movies added and adding new movies """
while counter > 0:
# generating a sequence
sequence = "00"
for _ in range(5):
sequence += choice(digits)
# Trying to get a movie with a uniqueID equal to our generated sequence
try:
movie = movieman.get_movie(sequence)
try:
# Getting the genres of the movie
genres = movie['genres']
languages = movie['languages']
movieID = int(sequence)
# Checking if there already a exists a movie with the same id
mc.execute(f"select count(distinct movieID) from movies where movieID={movieID}")
answer = mc.fetchall()
# If there are no duplicates then proceeding
if answer[0][0] == 0:
flag = 0
movieName = str(movie)
movieSummary = str(movie.summary())
for genre in genres:
for lang in languages:
sqlQuery = "insert into movies values ( ?, ?, ?, ?, ?)"
if lang!="None" and genre!="None":
try:
# Inserting values into the movies table in the database
mc.execute(sqlQuery,(movieID,movieName,genre,movieSummary,lang))
db.commit()
flag = 1
except sqlite3.ProgrammingError as e:
pass
if flag == 1:
# Decreasing the counter variable
counter -= 1
#Count variable used to show the total movies added so far
count += 1
print(count,"new movies added to the database.")
except KeyError as _:
pass
except IMDbDataAccessError as _:
pass
except IMDbDataAccessError as e:
pass
# Closing the connection when done
db.close()