forked from Dowsley/DragonCave-egg-catcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
catcher.py
141 lines (128 loc) · 4.14 KB
/
catcher.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
from json import load
from random import randrange
from time import sleep
from bs4 import BeautifulSoup
from robobrowser import RoboBrowser
import requests
HABITAT = {
'coast': 1,
'desert': 2,
'forest': 3,
'jungle': 4,
'alpine': 5,
'volcano': 6
}
# --- Retrieve credentials --- #
try:
f = open('secrets.json', 'r')
data = load(f)
f.close()
username = data['username']
password = data['password']
except Exception as e:
if type(e) == FileNotFoundError:
print('ERROR: Secrets.json file not found.')
else:
print('ERROR: Failed to retrieve credentials. '
+ 'Check for syntax errors in your secrets.json.')
exit(0)
# --- Retrieve habitats that are to be searched --- #
habitats = []
try:
f = open('habitats.txt', 'r')
lines = f.read().splitlines()
f.close()
# File must not be empty
if len(lines) == 0:
raise Exception('File Empty')
for i in lines:
h = i.lower().strip()
# Case 1: All habitats
if h == '*':
habitats = list(HABITAT.keys())
break
# Case 2: Unknown habitat
elif not (h in HABITAT.keys()):
print('ERROR: Habitat "{}" not recognized.'.format(h))
exit(0)
# Case 3: Normal habitat
habitats.append(h)
except Exception as e:
if type(e) == FileNotFoundError:
print('ERROR: habitats.txt file not found.')
else:
print('ERROR: Failed to retrieve the habitats. '
+ 'Check for errors in your habitats.txt.')
exit(0)
# --- Retrieve egg descriptions --- #
eggs = {}
try:
f = open('eggs.txt', 'r', encoding='utf8')
lines = f.read().splitlines()
f.close()
# File must not be empty
if len(lines) == 0:
raise Exception('File Empty')
for i in lines:
d = i.lower().replace('.', '').strip().split('=')
# Append to dictionary with the description as entry
eggs[d[0]] = int(d[1])
except Exception as e:
if type(e) == FileNotFoundError:
print('ERROR: eggs.txt file not found.')
else:
print('ERROR: Failed to retrieve the egg descriptions. '
+ 'Check for errors in your descriptions.txt.')
exit(0)
# --- Authentication --- #
try:
s = requests.Session()
s.headers['User-Agent'] = 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:39.0) Gecko/20100101 Firefox/39.0'
browser = RoboBrowser(session=s)
browser.open('https://dragcave.net/')
form = browser.get_form()
form['username'] = username
form['password'] = password
browser.submit_form(form)
print("-- SUCCESSFUL AUTHENTICATION --")
except:
print('Failed to authenticate. '
+ 'Check your credentials or Dragon Cave status')
exit(0)
# -- Overview message -- #
print(
'Starting with following entries:\n'
+ 'Habitats: {}\n'.format(', '.join(habitats))
+ 'Egg Orders:'
)
for i in eggs.items():
print('- {} ({})'.format(i[0], i[1]))
# --- Startup --- #
print('-- STARTING CATCHER --')
# Runs while the there are still orders
while eggs:
for h in habitats:
# Open and parse habitat
browser.open(
'https://dragcave.net/locations/'
+ str(HABITAT[h])
)
soup = BeautifulSoup(str(browser.parsed()), features='html.parser')
cave = (soup.find('div',class_='eggs')).findAll('div')
# Search available egg(s) in current habitat.
for egg in cave:
eggLink = "https://dragcave.net" + egg.find('a').get('href')
eggDesc = egg.find('span').text.lower().replace('.','')
print(eggLink, eggDesc)
# Catch dragon if it corresponds to a order.
if eggDesc in eggs:
browser.open(eggLink)
print('-- EGG FOUND -- ')
print('Description: {}'.format(eggDesc))
eggs[eggDesc] -= 1 # Decrement count
print('Left to catch: {}\n'.format(eggs[eggDesc]))
# If no eggs left to catch, remove entry from orders.
if eggs[eggDesc] == 0:
del eggs[eggDesc]
sleep(randrange(3))
print('-- EXECUTION FINISHED --')