-
Notifications
You must be signed in to change notification settings - Fork 1
/
user_choice.py
179 lines (152 loc) · 6.04 KB
/
user_choice.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
from bs4 import BeautifulSoup
import urllib.request
import datetime
import mysql.connector
from tabulate import tabulate
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
mydb = mysql.connector.connect(
host="localhost",
user="root",
passwd="####",
database="tvshows"
)
mycursor = mydb.cursor()
#We have created this database
# mycursor.execute("CREATE DATABASE tvshows")
#creating a table to store the email and users choice of shows
try:
mycursor.execute("CREATE TABLE User_Choice (Email VARCHAR(255), TV_Shows VARCHAR(255)")
mydb.commit()
except:
mydb.rollback()
#Add data to your table
def add_data(email, shows):
try:
mycursor.execute("INSERT INTO User_Choice(Email, TV_Shows) VALUES (email, shows);")
mydb.commit()
except:
mydb.rollback()
#Function to convert date in desired format so that it can be compared.
def convert_date(d):
month = {'Jan.':'01', 'Feb.':'02', 'Mar.':'03', 'Apr.':'04', 'May':'05',
'Jun.':'06', 'Jul.':'07', 'Aug.':'08', 'Sep.':'09', 'Oct.':'10',
'Nov.':'11', 'Dec.':'12'}
part = d.split()
new_date = part[0] + "/" + month[part[1]] + "/" + part[2]
return new_date
#Global variable which will store our HTML email message
html = ""
#main program
def main(entered_choice):
global html
choice = entered_choice
final_output = ""
words = choice.split()
updated_choice = ""
for word in words:
updated_choice = updated_choice + "+" + word
#To remove the inital + sign from the string
updated_choice = updated_choice[1:]
url = "https://www.imdb.com/find?q="+ updated_choice + "&ref_=nv_sr_sm"
page = urllib.request.urlopen(url)
soup = BeautifulSoup(page, 'html.parser')
right_table=soup.find('table', class_="findList")
ans = ""
rvs = ""
img_url = ""
title = ""
url = ""
show_url = "https://www.imdb.com"
#if the entered show exists or not
try:
row = right_table.find("tr")
cells = row.findAll('td', class_="result_text")
for cell in cells:
ans = show_url + cell.find('a').get('href')
url = ans #For storing the link of the show
page = urllib.request.urlopen(ans)
soup = BeautifulSoup(page, 'html.parser')
#Now we are on the particular shows page.
img_url = soup.find('div', class_="poster")
title = soup.find('div', class_="title_wrapper").h1.get_text(strip=True)
print(title)
#To deal with shows having no rating yet.
try:
rvs = soup.find('div', class_="ratingValue").get_text(strip=True)
except AttributeError:
rvs = "No reviews yet."
#If it's a movie then no episodes will exist
try:
episode_url = soup.find('a', class_='bp_item np_episode_guide np_right_arrow').get('href')
ans = show_url + episode_url
page = urllib.request.urlopen(ans)
soup = BeautifulSoup(page, 'html.parser')
current_date = datetime.datetime.now().strftime("%d/%m/%Y")
episode_date = soup.find_all('div', class_='airdate')
#finding next episode date
for date in episode_date:
date = date.get_text(strip=True)
#Making dates a datetime object so that they can be compared.
new_date = datetime.datetime.strptime(convert_date(date), "%d/%m/%Y")
curr_date = datetime.datetime.strptime(current_date, "%d/%m/%Y")
if new_date >= curr_date:
final_output = "Next episode: " + date
break
else:
#There could be a possibilty that there is a new season episode for the show
try:
season_url = soup.find('a', {'id' : 'load_next_episodes'}).get('href')
season_url = ans + season_url
page = urllib.request.urlopen(season_url)
soup = BeautifulSoup(page, 'html.parser')
new_season = soup.find('div', class_='airdate').get_text(strip=True)
final_output = "Next season: " + new_season
except:
final_output = "No new episodes/seasons upcoming yet!"
except:
final_output = "No information available"
#Checking if image for the show is available
try:
img_url = img_url.img.get('src')
except:
img_url = "https://bit.ly/2CMBIFr"
image = """<tr>
<td width="80" valign="top" bgcolor="d0d0d0" style="padding:5px;">
<img src=\"""" + img_url + """\" width="80" height="120"/>
</td>
<td width="15"></td>
<td valign="top">"""
except:
final_output = "This show doesn't exist."
#Making the final email message
title = """<h3>""" + """<a href=""" + url + """>""" + title + """</a><br>""" + """</h3>"""
final_output = """<p><em>""" + final_output + """</em></p>"""
reviews = """<p><em>""" + "Reviews: " + rvs + """</em></p>"""
series_content = image + """<b>""" + "TV series name : " + """</b>""" + title + final_output + reviews
html += series_content
#Functioning of the code begins here
user_email = input("Enter your email id: ")
shows_input = input("Enter the list of shows(seperated by comma): ")
shows_list = shows_input.split(',')
add_data(user_email, shows_input)
print("\nFetching data for shows...\n")
for show in shows_list:
main(show)
#SENDING EMAIL
try:
message = MIMEMultipart()
message["Subject"] = "Schedule of your favorite TV Shows"
message["From"] = sender_email
message["To"] = user_email
message.attach(MIMEText(html, "html"))
s = smtplib.SMTP('smtp.gmail.com')
s.starttls()
password = "sender_email_password"
s.login(sender_email, password)
s.sendmail(sender_email, user_email , message.as_string())
s.quit()
print("Email sent successfully!")
except:
print("Unable to send the email")