-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspell2csv.py
107 lines (93 loc) · 3.58 KB
/
spell2csv.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
import json
import re
import sys
import mysql.connector
#from bs4 import BeautifulSoup
import glob
from pprint import pprint
arguments=sys.argv[1:]
count_args=len(arguments)
if count_args!=1:
if count_args == 0:
print("spell2csv needs to have a directory argument")
elif count_args > 1:
print("spell2csv needs to have only one directory argument")
sys.exit(1)
print(sys.argv)
target_dir = sys.argv[1]
#print("I'm printing the target dir:")
#print(target_dir+"*.html")
listing = glob.glob(target_dir+"*.htm*")
#print("Printing the listing")
#print(listing)
for filename in listing:
# print(filename)
justname=re.search('[^/]+?\.htm',filename).string
pprint(justname)
with open(filename, encoding='Latin1') as htmfile:
# fulltext = ""
# for html in htmfile:
fulltext=htmfile.readlines() # result is a list
html = " ".join(fulltext)
reverse=False
reversible=0
# print("-----------------------------")
print(html)
# fulltext = fulltext+html
nameMatch =re.search(r'<p><font size=\"5\" color=\"#000000\"><u><b>(?P<name>.+?)</b></u></font></p>',html)
classMatch =re.search(r'Class\: <a href=\"(.*?)\">(?P<name>.+?)</a><br>',html)
levelMatch =re.search(r'Level: (?P<level>\d) <br>',html)
schoolMatch =re.search(r'School: (?P<school>.+?)<br>',html)
rangeMatch =re.search(r'Range: (?P<range>.+?)<br>',html)
componentsMatch =re.search(r'Components: (?P<components>.+?) <br>',html)
durationMatch =re.search(r'Duration: (?P<duration>.+?) <br>',html)
castTimeMatch =re.search(r'Casting Time: (?P<castTime>.+?) <br>',html)
areaMatch =re.search(r'Area of Effect: (?P<area>.+?) <br>',html)
saveMatch =re.search(r'Saving Throw: (P?<save>.+?) <br>',html)
descriptionMatch=re.search(r'<p><!-- Insert description under here-->.+?(?P<description>.+?)</p>',html)
if nameMatch:
spell_name=nameMatch.name
reverse =re.search(r'(?P<spell_name>.*?)- Reversible',spell_name)
print(spell_name)
if reverse:
spell_name=reverse.spell_name
reversible=1
if classMatch:
char_class=classMatch.name
print(char_class)
if levelMatch:
level=levelMatch.level
print(level)
if schoolMatch:
school=schoolMatch.school
print(school)
if rangeMatch:
spell_range = rangeMatch.range
print(spell_range)
if componentsMatch:
components=componentsMatch.components
print(components)
if durationMatch:
duration=durationMatch.duration
print(duration)
if castTimeMatch:
castTime=castTimeMatch.castTime
print(castTime)
if areaMatch:
area=areaMatch.area
print(area)
if saveMatch:
save=saveMatch.save
print(save)
if descriptionMatch:
description=descriptionMatch.description
description=re.sub(r'\n',' ',description)
print(description)
# print('insert into spells (class,level,name,school,range,components,duration,cast,area,save,description) values "'+'","'.join(char_class,level,spell_name,school,spell_range,components,duration,castTime,area,save,description)+'"')
mydb = mysql.connector.connect(
host="localhost",
user="root",
passwd="alatariel"
)
cursor = mydb.cursor()
#insert into spells (class,level,name,school,