forked from DedSecInside/TorBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlink.py
More file actions
141 lines (114 loc) · 3.6 KB
/
Copy pathlink.py
File metadata and controls
141 lines (114 loc) · 3.6 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
"""
This module is used to create a LinkNode that can be consumued by a LinkTree
and contains useful Link methods.
"""
import re
import requests
from bs4 import BeautifulSoup
from .color import color
from .validators import validate_email, validate_link
def get_emails(node):
"""Finds all emails associated with node
Args:
node (LinkNode): Node used to get emails from.
Returns:
emails (list): List of emails.
"""
emails = []
mails = re.findall(r'[\w\.-]+@[\w\.-]+', node._node.get_text())
for email in mails:
if validate_email(email):
emails.append(email)
return emails
def get_children(node):
children = []
for anchor_tag in node._node.find_all('a'):
link = anchor_tag.get('href')
if validate_link(link):
chlid_node = LinkNode(link)
children.append(chlid_node)
return children
def get_json_data(node):
"""Finds all link title associated with node
Args:
node (LinkNode): Node used to get links from.
Returns:
titles (list): List of Titles.
"""
json = []
for anchor_tag in node._node.find_all('a'):
link = anchor_tag.get('href')
json.append({"link":link,"tag":anchor_tag.string})
return json
def get_images(node):
"""Finds all images associated with node.
Args:
node (LinkNode): Node used to get links from.
Returns:
imageEls (list): A collection of img HTML elements
"""
imageEls = []
for anchor_tag in node._node.find_all('a'):
image = anchor_tag.get('src')
if validate_link(image):
imageEls.append(image)
return imageEls
class LinkNode:
"""Represents link node in a link tree."""
def __init__(self, link):
"""Initialises LinkNode object.
Args:
link (str): URL used to initialise node.
"""
# If link has invalid form, throw an error
if not validate_link(link):
raise ValueError("Invalid link format.")
self._loaded = False
self._name = link
self._link = link
self._emails = []
self._images = []
self._children = []
self._json_data = {}
def load_data(self):
response = requests.get(self._link)
status = str(response.status_code)
try:
response.raise_for_status()
self._metadata = response.headers
self._node = BeautifulSoup(response.text, 'html.parser')
self.status = color(status, 'green')
self._name = self._node.title.string
self._emails = get_emails(self)
self._children = get_children(self)
self._emails = get_emails(self)
self._images = get_images(self)
self._json_data = get_json_data(self)
except Exception:
self._node = None
self.status = color(status, 'yellow')
self._name = 'TITLE NOT FOUND'
finally:
self._loaded = True
def get_link(self):
return self._link
def get_name(self):
if not self._loaded:
self.load_data()
return self._name
def get_children(self):
if not self._loaded:
self.load_data()
return self._children
def get_emails(self):
if not self._loaded:
self.load_data()
return self._emails
def get_json(self):
if not self._loaded:
self.load_data()
return self._json_data
def get_metadata(self):
if not self._loaded:
self.load_data()
return self._metadata