-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
151 lines (121 loc) · 5.35 KB
/
main.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
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.action_chains import ActionChains
def initialize_driver():
"""
The initialize_driver function initializes a Chrome webdriver instance with the following options:
- --detached: This option allows the driver to stay open after the script has finished, while debugging.
:return: A webdriver object
:return: A webdriver object
:doc-author: Felipe Linares
"""
options = Options()
options.add_argument("--detached")
return webdriver.Chrome(options=options)
def access_tibia(driver):
"""
The access_tibia function opens the TIBIA website and returns the driver object.
:param driver: Webdriver
:return: The driver
:doc-author: Felipe Linares
"""
try:
# Open the TIBIA website
driver.get("https://www.tibia.com/community/?subtopic=houses")
finally:
# return driver
return driver
def select_world(driver, world_query, town_query):
"""
The select_world function takes in a driver and two queries, one for the world and one for the town. It then
navigates to tibia.com/community/?subtopic=worlds, selects the appropriate world from a dropdown menu,
selects an appropriate town from another dropdown menu (if there is more than one), clicks on "Rented
Houses" and submits all of this information to navigate to that specific world's rented houses page.
:param driver: Access the webdriver
:param world_query: Select the world that you want to search for houses in
:param town_query: Select the town that you want to search for houses in
:return: The driver with the new tab open
:doc-author: Felipe Linares
"""
driver = access_tibia(driver)
# Get Elements
windows = driver.window_handles
driver.switch_to.window(windows[0])
world_bar = driver.find_element(By.NAME, "world")
towns = driver.find_elements(By.NAME, "town")
rented = driver.find_element(By.CSS_SELECTOR,
'div#houses > div:nth-of-type(5) > div > div > form > div > table > tbody > tr > td '
'> div:nth-of-type(2) > table > tbody > tr:nth-of-type(2) > td > div > table > tbody '
'> tr:nth-of-type(2) > td:nth-of-type(2) > label:nth-of-type(3) > input')
submit = driver.find_element(By.CSS_SELECTOR, "input[value = 'Submit']")
# Perform actions
rented.click()
world_bar.send_keys(world_query + Keys.ENTER)
for town in towns:
print(town.get_attribute("value") + "is the town")
if town.get_attribute("value") == town_query:
town.click()
submit.click()
time.sleep(1)
navigate(driver, world_query)
break
return driver
def navigate(driver, world_query):
"""
The navigate function takes in a driver and world_query as parameters.
It then creates an ActionChains object to perform actions on the web page.
The function finds all the "View" buttons on the page, clicks them, and
then navigates through each tab that opens up to find any text that contains
the phrase "passed by house". If it does find this phrase, it writes this text
to a file called moving_out_{world_query}.txt.
:param driver: Pass the driver object to the function
:param world_query: Specify which world to search for
:return: The content of the views
:doc-author: Felipe Linares
"""
actions = ActionChains(driver)
# Get Views
views = driver.find_elements(By.CSS_SELECTOR, "input[value = 'View']")
# Click views
for view in views:
time.sleep(1)
actions.key_down(Keys.CONTROL).click(view).key_up(Keys.CONTROL).perform()
# Navigate tabs
windows = driver.window_handles
for window in windows:
driver.switch_to.window(window)
if driver.find_elements(By.CSS_SELECTOR, "input[value = 'Back']"):
content = driver.find_elements(By.TAG_NAME, "td")[1].text
with open("Output/output_" + world_query + ".txt", "a") as f:
f.write(content + "\n\n ---------------------------------------\n\n")
print(content)
if "pass the house" in content:
with open("Output/moving_out_" + world_query + ".txt", "a") as f:
f.write(content + "\n\n ---------------------------------------\n\n")
driver.close()
driver.switch_to.window(windows[0])
break
# Example usage
def main():
"""
The main function is the entry point of the program.
It initializes a driver, selects a world and town, and then quits.
:return: The driver
:doc-author: Felipe Linares
"""
with open("Towns", "r") as f:
towns = f.readlines()
with open("Worlds", "r") as f:
worlds = f.readlines()
for world in worlds:
for town in towns:
print(town)
driver1 = initialize_driver()
driver1 = select_world(driver1, world.rstrip("\n"), town.rstrip("\n"))
driver1.quit()
input("Press Enter to close the browser...")
if __name__ == "__main__":
main()