forked from m-haisham/webnovelbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.py
128 lines (93 loc) · 3.32 KB
/
example.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
import sys
from selenium.webdriver.common.action_chains import ActionChains
from temp import NOVEL, USER_EMAIL, USER_PASS
from webnovel import WebnovelBot
from webnovel.analytic import Efficient
from webnovel.api import UnlockType
from webnovel.exceptions import GuardException, CaptchaException
from webnovel.models import Novel
def slugify(s):
"""
Normalizes the string, converts to lowercase
"""
return "".join(x if x.isalnum() else "_" for x in s).lower()
def claim_daily(bot, novel=None):
# claim daily fast pass
bot.power_vote(novel)
bot.claim_tasks()
bot.energy_vote([1])
def format_title(s, l=20):
if len(s) > l:
return s[:l - 1] + '-'
return s + (' ' * (l - len(s)))
def show(analysis):
for c in analysis.via_coins:
c.ptype = 'coins'
for c in analysis.via_fastpass:
c.ptype = 'fastpass'
a = sorted(analysis.via_coins + analysis.via_fastpass, key=lambda c: c.no)
print()
print('Analysis report')
print('---------------')
for c in a:
print(f'no: {c.no}, title: {format_title(c.title)}, cost: {c.cost}, type: {c.ptype}')
print()
print('total cost')
print('----------')
print(f' coins: {analysis.coins_cost}')
print(f'fastpass: {analysis.fastpass_cost}')
def focus(chapter):
# get element
selector = f"li[data-cid='{chapter.id}']"
element = webnovel.driver.find_element_by_css_selector(selector)
# move cursor
ActionChains(webnovel.driver).move_to_element(element).perform()
def progress(c):
print(f'no: {c.no}, title: {format_title(c.title)}, cost: {c.cost}')
if __name__ == '__main__':
webnovel = WebnovelBot(timeout=360)
webnovel.driver.get(NOVEL)
novel_id = webnovel.novel_id
# signin throws `CaptchaException` and `GuardException` when the specified account cannot be found
# / redirected to guard
try:
webnovel.signin(USER_EMAIL, USER_PASS)
except CaptchaException:
sys.exit(0)
except GuardException:
sys.exit(0)
# claim_daily(webnovel, NOVEL)
profile = webnovel.profile()
api = webnovel.create_api()
webnovel.close()
# limit coin expenditure
# profile.coins = min(profile.coins, 70)
# analyser = ForwardCrawl(Novel(id=novel_id), profile, maximum_cost=10, on_load=progress)
analyser = Efficient(Novel(id=novel_id), profile, on_load=progress)
locked_chapters = [
chapter
for i, chapters in enumerate(api.toc(novel_id).values()) for chapter in chapters
if chapter.locked
]
print()
print('Analysing')
print('---------------')
analysis = analyser.analyse(locked_chapters)
show(analysis)
try:
input('\nRequesting permission to unlock chapters')
except KeyboardInterrupt:
print('Permission denied')
import sys
sys.exit()
print()
print('Unlocking')
print('---------')
for c in analysis.via_coins:
print(f'no: {c.no}, title: {format_title(c.title)}, cost: {c.cost}, type: coins ... ', end='')
api.unlock(c.novel_id_from_url(), c, UnlockType.coins)
print('unlocked')
for c in analysis.via_fastpass:
print(f'no: {c.no}, title: {format_title(c.title)}, cost: {c.cost}, type: fastpass ... ', end='')
api.unlock(c.novel_id_from_url(), c, UnlockType.fastpass)
print('unlocked')