-
Notifications
You must be signed in to change notification settings - Fork 0
/
inserter.py
103 lines (89 loc) · 4.25 KB
/
inserter.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
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
import logging
def login_phase(driver):
"""
@param driver: webdriver instance
@return: None
"""
driver.get("https://www.imdb.com/")
try:
user = WebDriverWait(driver, 60).until(EC.presence_of_element_located((By.CSS_SELECTOR,
"#imdbHeader > div.ipc-page-"
"content-container.ipc-page-"
"content-container--center."
"navbar__inner > "
"div._3cMNCrSVkxQhCkVs1JLIib.navbar__"
"user.sc-kgoBCf.iTQkiJ > div > "
"label.ipc-button.ipc-button--"
"single-padding.ipc-button--default-"
"height.ipc-button--core-baseAlt.ipc-"
"button--theme-baseAlt.ipc-button--"
"on-textPrimary.ipc-text-button.navbar__"
"flyout__text-button-after-mobile."
"navbar__user-menu-toggle__button "
"> div > span")))
print(f"Logged in as: {user.text}")
except TimeoutException:
print('Timeout exception - you have not logged in, please run the script again')
driver.close()
def searching_phase(driver, movie_title):
"""
@param driver: webdriver instance
@param movie_title: string
@return: None
"""
searching_bar = driver.find_element(By.CSS_SELECTOR, '#suggestion-search')
searching_bar.click()
searching_bar.send_keys(movie_title)
searching_bar.send_keys(Keys.ENTER)
def picking_movie_phase(driver, movie_title):
"""
@param driver: webdriver instance
@param movie_title: string
@return: None
"""
try:
movie_page = driver.find_element_by_link_text(movie_title)
except NoSuchElementException:
try:
movie_page = driver.find_element_by_xpath(f"//a[contains(text(),'{movie_title[0:5]}')]")
except NoSuchElementException:
try:
href = driver.find_element_by_xpath('//*[@id="main"]/div/div[2]/table/tbody/tr/td[2]/a')
movie_url = href.get_attribute('href')
except NoSuchElementException:
logging.info(f'Could not find any movie that matches search for: {movie_title}')
else:
driver.get(movie_url)
else:
movie_page.click()
else:
movie_page.click()
def rate_the_movie(driver, movie_title, rate):
"""
@param driver: webdriver instance
@param movie_title: string
@param rate: string
@return: None
"""
try:
rate_pool = driver.find_element(By.CSS_SELECTOR, "#star-rating-widget > div")
except NoSuchElementException:
logging.info(f'Could not find rating pool for: {movie_title}')
return False
else:
rate_pool.click()
try:
star_rate = driver.find_element_by_xpath(f'//*[@title="Click to rate: {rate}"]')
except NoSuchElementException:
logging.info(f'Could not find start pool for: {movie_title} and rate of: {rate}')
return False
else:
driver.implicitly_wait(1)
star_rate.click()
return True