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
80 lines (64 loc) · 2.12 KB
/
Copy pathcollect_data.py
File metadata and controls
80 lines (64 loc) · 2.12 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
79
80
"""
This module is used to gather data for analysis using thehiddenwiki.org.
"""
import datetime
import uuid
import requests
import os
from bs4 import BeautifulSoup
from threadsafe.safe_csv import SafeDictWriter
from progress.bar import Bar
from .utils import join_local_path
from .validators import validate_link
def parse_links(html):
"""Parses HTML page to extract links.
Args:
html (str): HTML block code to be parsed.
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):
"""Retrieve all meta elements from HTML object.
Args:
soup (BeautifulSoup)
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):
resp = requests.get(url)
links = parse_links(resp.text)
return links
default_url = 'https://thehiddenwiki.org'
def collect_data(user_url):
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(f'Processing...', max=len(links))
for link in links:
resp = requests.get(link)
soup = BeautifulSoup(resp.text, 'html.parser')
meta_tags = parse_meta_tags(soup)
entry = {
"ID": uuid.uuid4(),
"Title": soup.title.string,
"Metadata": meta_tags,
"Content": soup.find('body')
}
writer.writerow(entry)
bar.next()
bar.finish()
print(f'Data has been saved to {file_path}.')