This repository has been archived by the owner on May 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtravianApi.py
69 lines (53 loc) · 2.41 KB
/
travianApi.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
import datetime
from robobrowser import RoboBrowser
class TravianApi:
def __init__(self, user, pasw, WWVillage, serverLink, useragent):
self.serverLink = serverLink
self.browser = RoboBrowser(history=True,
parser='html.parser',
user_agent=useragent)
self.WWVillage_Name = WWVillage
self.browser.open(serverLink)
self.loggin(user, pasw)
def travianLink(self, destination=""):
if '.php' in destination:
return self.serverLink[:-9] + destination
return self.serverLink + destination
def loggin(self, user, pasw):
self.logActions('Opening login page (%s)' % self.travianLink())
self.browser.open(self.travianLink())
self.logActions("Logging as '%s'" % user)
form = self.browser.get_form(action='dorf1.php')
form["name"] = user
form["password"] = pasw
self.browser.session.headers['Referer'] = self.travianLink("dorf2.php")
self.browser.submit_form(form)
self.logActions("Logged in successfully!")
def openWWVillage(self):
try:
WWLink = self.browser.get_link(self.WWVillage_Name).get('href')
except:
print("Can NOT found WW Village named '"+self.WWVillage_Name+"'\nMake sure name is right.")
exit(0)
self.browser.open(self.travianLink(WWLink))
self.logActions("Navigated to '%s' (%s)" % (self.WWVillage_Name, WWLink))
def openMarketplace(self):
link = 'build.php?t=5&id=36'
self.browser.open(self.travianLink(link))
self.logActions('Opened up Marketplace (%s)' % self.travianLink(link))
def getSourcecode(self):
self.logActions("Source code has been copied and passed")
return self.browser.parsed
def checkIsCurrentVillageWW(self):
nameField = '#villageNameField'
currentVillage = ""
try:
currentVillage = self.browser.select(nameField)[0].text
isCurrVillageWW = self.WWVillage_Name in currentVillage
self.logActions('Is current village \'%s\' => %s' % (self.WWVillage_Name, isCurrVillageWW))
except Exception as e:
print("Something went wrong? (ID: "+nameField+")")
print(str(e))
return isCurrVillageWW
def logActions(self, action):
print('[%s | travianAPI] %s' % (datetime.datetime.now(), action))