-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap.py
134 lines (103 loc) · 3.69 KB
/
bootstrap.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
import re
from Utils.colors import print_string
from Utils.database import feed_data_into_db
from Utils.emailer import send_email_to_user
from TVShow.TVShow import TVShow
from colorama import init
from itertools import cycle
from multiprocessing.dummy import Pool as ThreadPool
NO_OF_WORKERS = 4
def get_input(text):
"""
Get input from user.
:param text: Text to print before taking input.
:return: Input of user.
"""
return input(text)
def get_tv_shows_list():
"""
Get list of TV shows as string input from user.
Then clean the input and convert the sting in a list.
:return: list of TV shows
"""
INPUT_STRING = 'Enter the list of TV shows seperated by comma(,): '
tv_shows = [x.strip().lower() for x in get_input(INPUT_STRING).split(',')]
return tv_shows
def get_user_input():
"""
Get input of user's email and list of TV Shows from command-line
:return: user's email, list of TV shows requested by user
"""
email_id = get_input("Enter your e-mail address: ")
email_id = validate_email(email_id)
tv_shows = get_tv_shows_list()
return email_id, tv_shows
def validate_email(email):
"""
Validate user_email by matching againt email RegEx.
Exit if email is invalid.
:param email: Email of user
:return: Validated email address
"""
if not re.match('^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$', email):
print('Email address ' + email + ' is not valid.')
exit(1)
return email
def get_show_status(tv_show_name):
"""
Get data regarding the next episode
:param tv_show_name: Name of TV Show
:return: Response/Result after fetching the data from internet
"""
return TVShow(tv_show_name).status
def processing(tv_show_list):
"""
Process and get the results for all the shows in the list
:param tv_show_list: List of all the TV Shows
:return: list of processed final responses for every TV Show
"""
# Make the Pool of workers
pool = ThreadPool(NO_OF_WORKERS)
# Fetches the status of TV shows in their own threads
# and return the results
results = pool.map(get_show_status, tv_show_list)
# close the pool and wait for the work to finish
pool.close()
pool.join()
return results
def main():
"""
Function which controls the flow of application.
It gets executed first.
Sequence of operations:
1. Fetch user input (email, list of TV Shows).
2. Feed data into database.
3. Fetch and process TV Show information from the internet.
4. Format and print the status of TV shows on the command-line.
5. Send the generated results to the user via email.
6. Exit gracefully.
"""
# Initiate colorama for colourful output
init()
# Iterator to toggle b/w 2 colours
colour_alternator = cycle(range(2))
# Initial body of email to be sent to user
email_body = ''
# Get email and list of TV Shows as input from user
user_email, tv_show_list = get_user_input()
# Record interaction in the data base along with inputs and timestamp
feed_data_into_db(user_email, tv_show_list)
# Fetch results for the list of TV Shows
results = processing(tv_show_list)
for idx, tv_show_response in enumerate(results):
stdout_str = str(idx+1) + '> ' + tv_show_response
# Print the status of each show on the terminal
print_string(stdout_str, colour_alternator)
# Append the status of each sting in the email body
email_body += stdout_str
# Send the status of TV Shows to the user via E-Mail
send_email_to_user(user_email, email_body)
# Exit Gracefully
exit(0)
if __name__ == "__main__":
main()