-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1-pathfinder.py
160 lines (137 loc) · 4.2 KB
/
1-pathfinder.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
153
154
155
156
157
158
159
160
#!/usr/bin/python3
# -*- coding: utf-8 -*-
###### Import ########
import json
import re
import requests
import signal
import sqlite3
import sys
from bs4 import BeautifulSoup
from pymongo import MongoClient
###### Variable ######
# File SQLite
file = 'pathfinder.sql'
# MongoDB configuration
server = 'localhost'
port = 27017
database = 'pathfinder'
column = 'spells'
# Web
url = "http://www.dxcontent.com/SDB_SpellBlock.asp?SDBID={0}"
sys.argv[1] = int(sys.argv[1])
###### Function ######
def signal_exit(sig, frame):
print('You pressed Ctrl + C !')
if sys.argv[1] == 2 or sys.argv[1] == 3:
closeSQLite(sqliteCon)
exit(0)
signal.signal(signal.SIGINT, signal_exit)
def flevel(stat,l=1):
level = (stat[0].text).split(";")[l][6:].split(",")
for i in range(len(level)):
wizard = re.findall('wizard',level[i])
if wizard:
num = re.findall('[0-9]',level[i])[0]
return num
try:
num = re.findall('[0-9]',level[0])[0]
except:
num = flevel(stat,2)
return num
def fcomponents(stat):
components = (stat[2].text)[10:].split(",")
l = []
for i in range(len(components)):
slash = re.findall('[/]',components[i])
char = re.findall('[A-Z]',components[i])
if slash:
component = components[i].strip()[:4]
l.append(component)
elif len(char) == 2:
component = components[i].strip()[:2]
l.append(component)
elif len(char) == 0:
continue
else:
component = re.findall('[A-Z]',components[i].strip())
l.append(component[0])
return l
def fresistance(stat):
resistance = (stat[-1].text).split(";")
for i in range(len(resistance)):
resist = re.findall('Spell Resistance',resistance[i])
if resist:
resist = resistance[i].strip()[16:].strip()
if resist == "see text":
return 'no'
else:
return resist
return 'no'
def conMongoDB(server, port, database, column):
# MongoDB
client = MongoClient(server, port)
db = client[database]
table = db[column]
return table
def conSQLite(column, file):
# SQLite
sqliteCon = sqlite3.connect(file)
cursor = sqliteCon.cursor()
sql = """
CREATE TABLE IF NOT EXISTS {0}(
id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE,
name TEXT NOT NULL,
level INTEGER NOT NULL,
components TEXT NOT NULL,
spell_resistance TEXT NOT NULL
)
""".format(column)
cursor.execute(sql)
sqliteCon.commit()
return sqliteCon, cursor
def insertMongoDB(table, spell):
result = table.insert_one(spell)
print('One post on MongoDB: {0}'.format(result.inserted_id))
return 0
def insertSQLite(cursor, spell):
spell['components'] = json.dumps(spell['components'])
cursor.execute("""INSERT INTO spells(name, level,components,spell_resistance) VALUES(:name, :level,:components,:spell_resistance)""", spell)
result = cursor.lastrowid
print('One post on SQLite: {0}'.format(result))
def closeSQLite(sqliteCon):
sqliteCon.commit()
sqliteCon.close()
###### Program #######
if sys.argv[1] == 1 or sys.argv[1] == 3 :
table = conMongoDB(server, port, database, column)
elif sys.argv[1] == 2 or sys.argv[1] == 3 :
sqliteCon, cursor = conSQLite(column, file)
else:
print('Select one argument, 1 for MongoDB, 2 for SQLite and 3 for MongoDB and SQLite')
exit(1)
for j in range(1, 1975):
if j == 1841 or j == 1972:
continue
page = requests.get(url.format(j))
soup = BeautifulSoup(page.text, 'html.parser')
print
name = soup.find(class_='heading').text
stat = soup.find_all(class_='SPDet')
level = flevel(stat)
components = fcomponents(stat)
resistance = fresistance(stat)
spell = {
"name": name,
"level": level,
"components" : components,
"spell_resistance" : resistance
}
# print(spell)
if sys.argv[1] == 1 or sys.argv[1] == 3 :
insertMongoDB(table, spell)
elif sys.argv[1] == 2 or sys.argv[1] == 3 :
insertSQLite(cursor, spell)
if sys.argv[1] == 2 or sys.argv[1] == 3:
closeSQLite(sqliteCon)
exit(0)