-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
87 lines (65 loc) · 2.33 KB
/
utils.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
from os import path
from os import mkdir
import json
import unicodedata
import re
from definitions import PYTHON_APP_DIR
pathToRecipeLinks = path.join(PYTHON_APP_DIR, "output", "_recipeLinks.json")
pepperplateDir = "~/Pepperplate"
def slugify(value):
"""
Convert to ASCII. Convert spaces to hyphens.
Remove characters that aren't alphanumerics, underscores, or hyphens.
Convert to lowercase. Also strip leading and trailing whitespace.
"""
value = str(value)
value = unicodedata.normalize('NFKD', value).encode(
'ascii', 'ignore').decode('ascii')
value = re.sub(r'[^\w\s]', '', value).strip()
return re.sub(r'[-\s]+', '', value.title())
prevSlug = ""
suffix = 2
def generateFileName(title):
global prevSlug, suffix
fileName = slugify(title)
if fileName == prevSlug:
fileName = f"{fileName}{suffix}"
suffix += 1
else:
prevSlug = fileName
suffix = 2
return fileName
def saveRecipeAsJson(recipe, fileName):
with open(path.join(path.expanduser(pepperplateDir), "j", f"{fileName}.json"), "a") as f:
json.dump(recipe, f, indent=3)
def saveRecipeLinks(links):
with open(pathToRecipeLinks, "a") as f:
json.dump(links, f, indent=3)
def createDirectories():
print("Creating folders in ~/Pepperplate...")
createDirectory(['~', "Pepperplate"])
createDirectory(['~', "Pepperplate", "j"])
createDirectory(['~', "Pepperplate", "p"])
def createDirectory(pathPieces):
try:
mkdir(path.expanduser('/'.join(pathPieces)))
print(f"Directory {'/'.join(pathPieces)} created!")
except FileExistsError:
print(f"Directory {'/'.join(pathPieces)} already exists!")
def getRecipeLinks(crawler):
if path.exists(pathToRecipeLinks):
with open(pathToRecipeLinks, "r") as f:
return json.load(f), "Recipe Links have already been scraped!"
else:
recipeLinks = crawler.FetchRecipeLinks()
saveRecipeLinks(recipeLinks)
return recipeLinks, "Finished scraping recipe links!"
def askFormat():
while True:
format = input(
"What export format would you like:\n[j]son\n[p]ng screenshots\n[b]oth\n>> ")
format = format.strip()
if format in "jbp":
return format
else:
print("Enter `j` for JSON, `p` for PDF, `b` for both.")