-
Notifications
You must be signed in to change notification settings - Fork 0
/
urlhelper.py
156 lines (140 loc) · 4.59 KB
/
urlhelper.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
152
"""
A simple class that will handle URL requests
"""
from urllib.request import Request, urlopen
import urllib.error
import re
import help_messages
from bs4 import *
"""
Checks if the given url exists by reading the response code
"""
def url_exits(url):
code = 0
try:
req = Request(url, headers={'User-Agent': 'Mozilla/5.0'})
webpage = urlopen(req)
code = webpage.getcode()
except urllib.error.HTTPError as e:
pass
if code == 200:
return True
else:
return False
"""
Finds the card's image url
"""
def get_card_image(url, cardname):
imgpostfix = None
try:
req = Request(url+"png", headers={'User-Agent': 'Mozilla/5.0'})
source = urlopen(req).read()
soup = BeautifulSoup(source, "html.parser")
imgpostfix = soup.find("img").get("src")
return help_messages.card_img_prefix + imgpostfix
except urllib.error.HTTPError as e:
pass
try:
req = Request(url+"jpg", headers={'User-Agent': 'Mozilla/5.0'})
source = urlopen(req).read()
soup = BeautifulSoup(source, "html.parser")
imgpostfix = soup.find("img").get("src")
return help_messages.card_img_prefix + imgpostfix
except urllib.error.HTTPError as e:
pass
try:
req = Request(url+"jpeg", headers={'User-Agent': 'Mozilla/5.0'})
source = urlopen(req).read()
soup = BeautifulSoup(source, "html.parser")
imgpostfix = soup.find("img").get("src")
return help_messages.card_img_prefix + imgpostfix
except urllib.error.HTTPError as e:
pass
return None
"""
Find the game's info
"""
def game_info(game_url):
info = []
try:
req = Request(game_url, headers={'User-Agent': 'Mozilla/5.0'})
webpage = urlopen(req).read()
except urllib.error.HTTPError as e:
pass
soup = BeautifulSoup(webpage, "html.parser")
for h in soup.find_all("h2",{"class" : "page-title"}):
info.append(h.string)
info.append(soup.p.get_text(" ", strip=True))
want = True
counter = 0
for heading in soup.find_all("td"):
if want and counter < 3:
info.append(heading.get_text(" ", strip=True))
want = False
counter += 1
else:
want = True
info.append(soup.img.get('src'))
return info
"""
Find the booster's info
"""
def booster_info(booster_url):
info = []
try:
req = Request(booster_url, headers={'User-Agent': 'Mozilla/5.0'})
webpage = urlopen(req).read()
except urllib.error.HTTPError as e:
pass
soup = BeautifulSoup(webpage, "html.parser")
info.append(soup.find_all("h1",{"class":"page-header__title"})[0].get_text(" ", strip=True))
info.append(soup.find_all("p")[0].get_text(" ", strip=True))
info.append(soup.find_all("p")[1].get_text(" ", strip=True))
info.append(soup.find_all('div', id='mw-content-text')[0].ul.get_text(" ", strip=True))
link = soup.find_all("a",{"class":"image-thumbnail"})[0]
info.append(link.get("href"))
info.append(soup.find_all("div",{"class":"pi-data-value"})[4].get_text(" ", strip=True))
return info
"""
Find the deck info
"""
def deck_info(deck_url):
info = []
try:
req = Request(deck_url, headers={'User-Agent': 'Mozilla/5.0'})
webpage = urlopen(req).read()
except urllib.error.HTTPError as e:
pass
soup = BeautifulSoup(webpage, "html.parser")
info.append(soup.find_all("h1",{"class":"page-header__title"})[0].get_text(" ", strip=True))
info.append(soup.find_all("p")[0].get_text(" ", strip=True))
info.append(soup.find_all("p")[1].get_text(" ", strip=True))
info.append(soup.find_all('div', id='mw-content-text')[0].ul.get_text(" ", strip=True))
link = soup.find_all("a",{"class":"image-thumbnail"})[0]
info.append(link.get("href"))
info.append(soup.find_all("div",{"class":"pi-data-value"})[4].get_text(" ", strip=True))
return info
"""
Find the color info
"""
def get_color_info(color_url):
info = []
color_data = []
try:
req = Request(color_url, headers={'User-Agent': 'Mozilla/5.0'})
webpage = urlopen(req).read()
except urllib.error.HTTPError as e:
pass
soup = BeautifulSoup(webpage, "html.parser")
paragraphs = soup.find_all("p")
for p in paragraphs:
info.append(p.get_text(" ", strip=True))
if len(info) == 2:
color_data.append(info[0])
else:
color_data.append(info[0])
if len(info[1]) > 997:
info[1] = info[1][:997]
info[1] = info[1] + "..."
color_data.append(info[1])
return color_data