Skip to content

Commit

Permalink
refactored, added multi hub support, changed colors
Browse files Browse the repository at this point in the history
  • Loading branch information
iesus committed Jun 6, 2020
1 parent 0b6b4b8 commit 6afca20
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 137 deletions.
71 changes: 71 additions & 0 deletions _scrape.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import networkx as nx
import re
import requests
from bs4 import BeautifulSoup

G = nx.Graph()
cookies = {'JSESSIONID': input("COOKIE:")}
host = 'https://graph-eu01-euwest1.api.smartthings.com'
r = requests.get(host+'/device/list', cookies=cookies)

soup = BeautifulSoup(r.content, "html.parser")
translationDict = {}
for device in soup.select('#device-table > tbody > tr'):
link = device.select('td:nth-of-type(1) > a')[0]
deviceName = link.text.strip()
deviceDetailsLink = link.get('href')
deviceType = device.select('td:nth-of-type(2)')[0].text.strip()
hubName = device.select('td:nth-of-type(4)')[0].text.strip()
deviceId = device.select('td:nth-of-type(5)')[0].text.strip()
deviceNetworkId = device.select('td:nth-of-type(6)')[0].text.strip()
deviceStatus = device.select('td:nth-of-type(7)')[0].text.strip()
G.add_node(hubName, details='{\'name\': hubName}')

deviceDetails = requests.get(host+deviceDetailsLink, cookies=cookies)
details = BeautifulSoup(deviceDetails.content, "html.parser")
translationDict[deviceNetworkId] = deviceName
translationDict[hubName] = hubName
deviceData = {
'name': deviceName,
'Type': deviceType,
'ID': deviceId,
'NID': deviceNetworkId,
'Status': deviceStatus
}
G.add_node(deviceNetworkId, details=deviceData)
routes = details.select('#meshRoute-label + td a')
deviceRoute = []
for route in routes:
rex = re.search('.*\((.+)\).*', route.text)
if route == routes[1]:
print(deviceNetworkId + ' ' + deviceName + ' is connected to:')

if rex and rex.group(1) != deviceNetworkId:
deviceRoute.append(rex.group(1))
print(rex.group(1))

if route.text == hubName:
deviceRoute.append(hubName)
print(route.text)

if route == routes[-1]:
print("\n")

previousroute = None
for route in deviceRoute:
if not previousroute:
G.add_edge(deviceNetworkId, route)
else:
G.add_edge(route, previousroute)
previousroute = route

options = {
'font_size': 10,
'node_size': 100,
'width': 2,
'with_labels': True,
'labels': translationDict,
'node_color': '#DE781F',
'edge_color': '#1F85DE'
}
nx.draw(G,**options)
70 changes: 1 addition & 69 deletions scrape-st-save-image.py
Original file line number Diff line number Diff line change
@@ -1,74 +1,6 @@
import networkx as nx
import re
import requests
from bs4 import BeautifulSoup
import _scrape
import matplotlib.pyplot as plt

G = nx.Graph()
cookies = {'JSESSIONID': input("COOKIE:")}
host = 'https://graph-eu01-euwest1.api.smartthings.com'
r = requests.get(host+'/device/list', cookies=cookies)

G.add_node('0',details='{\'name\': \'Hub\'}')

soup = BeautifulSoup(r.content, "html.parser")
translationDict = {}
for device in soup.select('#device-table > tbody > tr'):
link = device.select('td:nth-of-type(1) > a')[0]
deviceName = link.text.strip()
deviceDetailsLink = link.get('href')

deviceType = device.select('td:nth-of-type(2)')[0].text.strip()
hubName = device.select('td:nth-of-type(4)')[0].text.strip()
deviceId = device.select('td:nth-of-type(5)')[0].text.strip()
deviceNetworkId = device.select('td:nth-of-type(6)')[0].text.strip()
deviceStatus = device.select('td:nth-of-type(7)')[0].text.strip()

deviceDetails = requests.get(host+deviceDetailsLink, cookies=cookies)
details = BeautifulSoup(deviceDetails.content, "html.parser")
translationDict[deviceNetworkId] = deviceName
deviceData = {
'name': deviceName,
'Type': deviceType,
'ID': deviceId,
'NID': deviceNetworkId,
'Status': deviceStatus
}
G.add_node(deviceNetworkId, details=deviceData)
routes = details.select('#meshRoute-label + td a')
deviceRoute = []
for route in routes:
rex = re.search('.*\((.+)\).*', route.text)
if route == routes[1]:
print(deviceNetworkId + ' ' + deviceName + ' is connected to:')

if rex and rex.group(1) != deviceNetworkId:
deviceRoute.append(rex.group(1))
print(rex.group(1))
if route.text == hubName:
deviceRoute.append('0')
print(route.text)
if route == routes[-1]:
print("\n")

previousroute = None
for route in deviceRoute:
if not previousroute:
G.add_edge(deviceNetworkId, route)
else:
G.add_edge(route, previousroute)
previousroute = route


options = {
'font_size': 10,
'node_size': 50,
'width': 2,
'with_labels': True,
'labels': translationDict
}

nx.draw(G,**options)
fig = plt.gcf()
fig.set_size_inches(180, 100, forward=True)
plt.savefig('path.png')
69 changes: 1 addition & 68 deletions scrape-st.py
Original file line number Diff line number Diff line change
@@ -1,73 +1,6 @@
import networkx as nx
import re
import requests
from bs4 import BeautifulSoup
import _scrape
import matplotlib.pyplot as plt

G = nx.Graph()
cookies = {'JSESSIONID': input("COOKIE:")}
host = 'https://graph-eu01-euwest1.api.smartthings.com'
r = requests.get(host+'/device/list', cookies=cookies)

G.add_node('0',details='{\'name\': \'Hub\'}')

soup = BeautifulSoup(r.content, "html.parser")
translationDict = {}
for device in soup.select('#device-table > tbody > tr'):
link = device.select('td:nth-of-type(1) > a')[0]
deviceName = link.text.strip()
deviceDetailsLink = link.get('href')
deviceType = device.select('td:nth-of-type(2)')[0].text.strip()
hubName = device.select('td:nth-of-type(4)')[0].text.strip()
deviceId = device.select('td:nth-of-type(5)')[0].text.strip()
deviceNetworkId = device.select('td:nth-of-type(6)')[0].text.strip()
deviceStatus = device.select('td:nth-of-type(7)')[0].text.strip()

deviceDetails = requests.get(host+deviceDetailsLink, cookies=cookies)
details = BeautifulSoup(deviceDetails.content, "html.parser")
translationDict[deviceNetworkId] = deviceName
deviceData = {
'name': deviceName,
'Type': deviceType,
'ID': deviceId,
'NID': deviceNetworkId,
'Status': deviceStatus
}
G.add_node(deviceNetworkId, details=deviceData)
routes = details.select('#meshRoute-label + td a')
deviceRoute = []
for route in routes:
rex = re.search('.*\((.+)\).*', route.text)
if route == routes[1]:
print(deviceNetworkId + ' ' + deviceName + ' is connected to:')

if rex and rex.group(1) != deviceNetworkId:
deviceRoute.append(rex.group(1))
print(rex.group(1))
if route.text == hubName:
deviceRoute.append('0')
print(route.text)
if route == routes[-1]:
print("\n")

previousroute = None
for route in deviceRoute:
if not previousroute:
G.add_edge(deviceNetworkId, route)
else:
G.add_edge(route, previousroute)
previousroute = route


options = {
'font_size': 10,
'node_size': 50,
'width': 2,
'with_labels': True,
'labels': translationDict
}

nx.draw(G,**options)
fig = plt.gcf()
fig.set_size_inches(180, 100, forward=True)
plt.show()
Binary file modified screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 6afca20

Please sign in to comment.