forked from DedSecInside/TorBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollect_data.py
More file actions
78 lines (64 loc) · 2.28 KB
/
Copy pathcollect_data.py
File metadata and controls
78 lines (64 loc) · 2.28 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
"""
This module is used to gather data for analysis using thehiddenwiki.org.
"""
import datetime
import uuid
import requests
from bs4 import BeautifulSoup
from progress.bar import Bar
from threadsafe.safe_csv import SafeDictWriter
from .utils import join_local_path
from .validators import validate_link
from .log import debug
def parse_links(html: str):
"""Parses HTML page to extract links.
Returns:
(list): List of all valid links found.
"""
soup = BeautifulSoup(html, 'html.parser')
tags = soup.find_all('a')
return [tag['href'] for tag in tags if validate_link(tag['href'])]
def parse_meta_tags(soup: BeautifulSoup):
"""Retrieve all meta elements from HTML object.
Returns:
list: List containing content from meta tags
"""
meta_tags = soup.find_all('meta')
content_list = list()
for tag in meta_tags:
content_list.append(tag.attrs)
return content_list
def get_links(url: str):
resp = requests.get(url)
links = parse_links(resp.text)
return links
default_url = 'https://thehiddenwiki.org'
def collect_data(user_url: str):
url = user_url if user_url is not None else default_url
print(f"Gathering data for {url}")
links = get_links(url)
current_time = datetime.datetime.now().isoformat()
file_name = f'torbot_{current_time}.csv'
file_path = join_local_path(file_name)
with open(file_path, 'w+') as outcsv:
fieldnames = ['ID', 'Title', 'Metadata', 'Content']
writer = SafeDictWriter(outcsv, fieldnames=fieldnames)
bar = Bar('Processing...', max=len(links))
for link in links:
try:
resp = requests.get(link)
soup = BeautifulSoup(resp.text, 'html.parser')
meta_tags = parse_meta_tags(soup)
entry = {
"ID": uuid.uuid4(),
"Title": soup.title.string if soup.title else "",
"Metadata": meta_tags,
"Content": soup.find('body')
}
writer.writerow(entry)
except requests.exceptions.RequestException as e:
debug(e)
debug(f"Failed to connect to [{link}].")
bar.next()
bar.finish()
print(f'Data has been saved to {file_path}.')