Last active
May 21, 2023 09:15
-
-
Save farooqkz/62804f6d229f2069e93fec932fa97686 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import requests | |
import networkx as nx | |
import pygraphviz as pgv | |
MAX_DEPTH = 3 | |
ADDR_PATTERN = re.compile("[a-zA-Z0-9\-]+\.blog\.ir", re.M) | |
G = nx.DiGraph() | |
def add_all_links(addr, d=0): | |
global ADDR_PATTERN | |
global G | |
if d >= MAX_DEPTH: | |
return | |
name = addr.split(".")[0] | |
if not addr.startswith("http"): | |
addr = "http://" + addr | |
try: | |
ok = requests.head(addr).ok | |
except: | |
return | |
if not ok: | |
return | |
links = set(ADDR_PATTERN.findall(requests.get(addr).content.decode())) | |
for link in links: | |
G.add_edge(name, link.split(".")[0]) | |
add_all_links(link, d+1) | |
add_all_links("pcworms.blog.ir") | |
with open("/tmp/atmp", "w") as fp: | |
nx.drawing.nx_pydot.write_dot(G, fp) | |
del G | |
G = pgv.AGraph("/tmp/atmp") | |
G.layout() | |
G.draw("/tmp/gout.png") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment