-
Notifications
You must be signed in to change notification settings - Fork 0
/
general.py
53 lines (40 loc) · 1.13 KB
/
general.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
import os
# Each website is a separate project
def create_project_dir(directory):
if not os.path.exists(directory):
print("Creating project " + directory)
os.makedirs(directory)
# Create queue and crawled files
def create_data_files(project_name, base_url):
queue = project_name + '/queue.txt'
crawled = project_name + '/crawled.txt'
if not os.path.isfile(queue):
file_write(queue, base_url)
if not os.path.exists(crawled):
file_write(crawled, '')
# Add a new record
def file_write(path, data):
f = open(path, 'w')
f.write(data)
f.close()
# Append to a file
def file_append(path, data):
file = open(path, 'a')
file.write(data + '\n')
file.close()
# Delete file contents
def file_delete(path):
with open(path, 'w'):
pass
# Read a file and convert to set
def file_to_set(path):
links = set()
with open(path, 'r') as f:
for line in f:
links.add(line.replace('\n',''))
return links
# Convert set to file
def set_to_file(links, file):
file_delete(file)
for link in sorted(links):
file_append(file, link)