This repository has been archived by the owner on Feb 1, 2018. It is now read-only.
forked from vomz/swgoh_code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw_util.py
80 lines (65 loc) · 2.42 KB
/
sw_util.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
from __future__ import print_function
import time
from collections import namedtuple
import requests
from bs4 import BeautifulSoup as Soup
from pprint import pprint
def timeit(method):
def timed(*args, **kw):
ts = time.time()
result = method(*args, **kw)
te = time.time()
print('%r (%r, %r) %2.2f sec' % (method.__name__, args, kw, te - ts))
return result
return timed
class Collections:
Toon = namedtuple('Toon', 'name link star level gear')
@timeit
def __init__(self, user):
self.session = requests.session()
self.user = user
r = self.session.get('https://swgoh.gg/u/{}/collection'.format(user))
self.collection = Soup(r.text, 'html.parser')
# collect the characters that are unlocked
self.toons = []
self._populate_toons()
def _populate_toons(self):
raw = self.collection.find_all('div', 'col-xs-6 col-sm-3 col-md-3 col-lg-2')
for cur_toon in raw:
name = cur_toon.find("div", {"class": "collection-char-name"}).text
link = cur_toon.find('a', 'collection-char-name-link')['href']
try:
level = int(cur_toon.find("div", {"class": "char-portrait-full-level"}).text)
inactive = len(cur_toon.find_all("div", {"class": 'star-inactive'}))
star = len(cur_toon.find_all("div", {"class": 'star'})) - inactive
gear = cur_toon.find("div", {"class": "char-portrait-full-gear-level"}).text
except AttributeError:
# character not owned
level = 0
star = 0
gear = 0
self.toons.append(Collections.Toon(name, link, star, level, gear))
@property
def num_toons(self):
num_toons = len(self.toons)
return num_toons
def owned_toons(self, star=1, exact=False):
oper = '==' if exact else '>='
comp = '[toon for toon in self.toons if toon.star {} {}]'.format(oper, star)
return eval(comp)
@property
def missing_toons(self):
return self.owned_toons(star=0, exact=True)
def main():
'''
Demo
:return:
'''
user = Collections('w0m')
print(user.num_toons)
print(len(user.owned_toons(star=5, exact=True)))
print(pprint(user.owned_toons(star=5, exact=True)))
print(len(user.owned_toons()))
print(len(user.missing_toons))
if __name__ == "__main__":
main()