-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.py
More file actions
59 lines (46 loc) · 1.61 KB
/
Main.py
File metadata and controls
59 lines (46 loc) · 1.61 KB
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
import sys
import json
from pathlib import Path
from CreatureScrapper import CreatureScrapper
from ListScrapper import ListScrapper
VERSION = '1.1'
AVAILABLE_COMMANDS = ['creature', 'list']
TIBIA_FANDOM_URL = 'https://tibia.fandom.com/wiki/List_of_Creatures'
def scrapCreatures():
print(f'Scrapping creatures from `in.txt` file')
inputFilePath = 'in.txt'
if not Path(inputFilePath).is_file():
print(f'Unable to find file with URLs to scrap. Create `{inputFilePath}`')
quit()
urls = []
with open(inputFilePath) as f:
lines = f.readlines()
for line in lines:
urls.append(line.replace('\n', ''))
creatures = []
for url in urls:
creatureScrapper = CreatureScrapper()
creature = creatureScrapper.scrap(url)
creatures.append(creature)
json_string = json.dumps(
[ob.__dict__ for ob in creatures],
sort_keys=True,
indent=4
)
with open("out.json", "w") as file:
file.write(json_string)
def scrapList():
print(f'Scrapping list of creatures from tibia.fandom')
listScrapper = ListScrapper(TIBIA_FANDOM_URL)
listEntries = listScrapper.scrap()
with open('list.txt', 'w') as file:
file.writelines(entry + "\n" for entry in listEntries)
print(f'*** Tibia-Fandom-Web-Scrapper v. {VERSION} ***')
if len(sys.argv) != 2 or sys.argv[1] not in AVAILABLE_COMMANDS:
print(f'You have to choose of the available commands: {AVAILABLE_COMMANDS}')
sys.exit()
command = sys.argv[1]
if command == AVAILABLE_COMMANDS[0]:
scrapCreatures()
elif command == AVAILABLE_COMMANDS[1]:
scrapList()