Skip to content

SpiderFoot automates OSINT for threat intelligence and mapping your attack surface.

Move API queries into a query method

Brendan Coles49506912e961
diff --git a/modules/sfp_spyonweb.py b/modules/sfp_spyonweb.py
index 0b722709..57852a4e 100644
--- a/modules/sfp_spyonweb.py
+++ b/modules/sfp_spyonweb.py
@@ -11,11 +11,11 @@
# Licence: GPL
# -------------------------------------------------------------------------------
+import datetime
import json
import re
-import time
-import datetime
import socket
+import time
from sflib import SpiderFoot, SpiderFootPlugin, SpiderFootEvent
class sfp_spyonweb(SpiderFootPlugin):
@@ -82,17 +82,17 @@ class sfp_spyonweb(SpiderFootPlugin):
def producedEvents(self):
return ['CO_HOSTED_SITE', 'INTERNET_NAME', 'AFFILIATE_DOMAIN', 'WEB_ANALYTICS_ID']
- # Retrieve hosts with the specified Google Analytics ID
+ # Query the REST API
# https://api.spyonweb.com/v1/docs
- def queryGoogleAnalytics(self, qry, limit=100):
- url = "https://api.spyonweb.com/v1/analytics/" + qry
+ def query(self, endpoint, qry, limit=100):
+ url = "https://api.spyonweb.com/v1/" + endpoint + "/" + qry
url += "?limit=" + str(limit)
url += "&access_token=" + self.opts['api_key']
res = self.sf.fetchUrl(url, timeout=self.opts['timeout'], useragent=self.opts['_useragent'])
if res['content'] is None:
- self.sf.info("No results found for " + qry)
+ self.sf.debug("No results found for " + qry)
return None
try:
@@ -102,69 +102,40 @@ class sfp_spyonweb(SpiderFootPlugin):
return None
status = data.get('status')
- if not status == 'found':
- self.sf.info("No results found for " + qry)
- return None
-
- if 'result' not in data:
- self.sf.info("No results found for " + qry)
- return None
-
- if 'analytics' not in data['result']:
- self.sf.info("No results found for " + qry)
- return None
- if qry not in data['result']['analytics']:
- self.sf.info("No results found for " + qry)
+ if not status == 'found':
+ self.sf.debug("No results found for " + qry)
return None
- items = data['result']['analytics'][qry].get('items')
+ api_result = data.get('result')
- if not items:
+ if not api_result:
self.sf.debug("No results found for " + qry)
return None
- self.sf.info("Retrieved " + str(len(items)) + " results")
-
- return items
-
- # Retrieve hosts with the specified Google AdSense ID
- # https://api.spyonweb.com/v1/docs
- def queryGoogleAdsense(self, qry, limit=100):
- url = "https://api.spyonweb.com/v1/adsense/" + qry
- url += "?limit=" + str(limit)
- url += "&access_token=" + self.opts['api_key']
-
- res = self.sf.fetchUrl(url, timeout=self.opts['timeout'], useragent=self.opts['_useragent'])
+ endpoint_result = api_result.get(endpoint)
- if res['content'] is None:
- self.sf.info("No results found for " + qry)
+ if not endpoint_result:
+ self.sf.debug("No results found for " + qry)
return None
- try:
- data = json.loads(res['content'])
- except Exception as e:
- self.sf.debug("Error processing JSON response.")
- return None
+ results = endpoint_result.get(qry)
- status = data.get('status')
- if not status == 'found':
- self.sf.info("No results found for " + qry)
+ if not results:
+ self.sf.debug("No results found for " + qry)
return None
- if 'result' not in data:
- self.sf.info("No results found for " + qry)
- return None
+ items = results.get('items')
- if 'adsense' not in data['result']:
- self.sf.info("No results found for " + qry)
+ if not items:
+ self.sf.debug("No results found for " + qry)
return None
- if qry not in data['result']['adsense']:
- self.sf.info("No results found for " + qry)
- return None
+ return items
- items = data['result']['adsense'][qry].get('items')
+ # Retrieve hosts with the specified Google Analytics ID
+ def queryGoogleAnalytics(self, qry, limit=100):
+ items = self.query('analytics', qry, limit)
if not items:
self.sf.debug("No results found for " + qry)
@@ -174,43 +145,21 @@ class sfp_spyonweb(SpiderFootPlugin):
return items
- # Retrieve hosts on the specified IP address
- # https://api.spyonweb.com/v1/docs
- def queryIP(self, qry, limit=100):
- url = "https://api.spyonweb.com/v1/ip/" + qry
- url += "?limit=" + str(limit)
- url += "&access_token=" + self.opts['api_key']
-
- res = self.sf.fetchUrl(url, timeout=self.opts['timeout'], useragent=self.opts['_useragent'])
-
- if res['content'] is None:
- self.sf.info("No results found for " + qry)
- return None
-
- try:
- data = json.loads(res['content'])
- except Exception as e:
- self.sf.debug("Error processing JSON response.")
- return None
-
- status = data.get('status')
- if not status == 'found':
- self.sf.info("No results found for " + qry)
- return None
+ # Retrieve hosts with the specified Google AdSense ID
+ def queryGoogleAdsense(self, qry, limit=100):
+ items = self.query('adsense', qry, limit)
- if 'result' not in data:
- self.sf.info("No results found for " + qry)
+ if not items:
+ self.sf.debug("No results found for " + qry)
return None
- if 'ip' not in data['result']:
- self.sf.info("No results found for " + qry)
- return None
+ self.sf.info("Retrieved " + str(len(items)) + " results")
- if qry not in data['result']['ip']:
- self.sf.info("No results found for " + qry)
- return None
+ return items
- items = data['result']['ip'][qry].get('items')
+ # Retrieve hosts with the specified IP address
+ def queryIP(self, qry, limit=100):
+ items = self.query('ip', qry, limit)
if not items:
self.sf.debug("No results found for " + qry)
@@ -221,42 +170,8 @@ class sfp_spyonweb(SpiderFootPlugin):
return items
# Retrieve Google Analytics and Google AdSense IDs for the specified domain
- # https://api.spyonweb.com/v1/docs
def querySummary(self, qry, limit=100):
- url = "https://api.spyonweb.com/v1/summary/" + qry
- url += "?limit=" + str(limit)
- url += "&access_token=" + self.opts['api_key']
-
- res = self.sf.fetchUrl(url, timeout=self.opts['timeout'], useragent=self.opts['_useragent'])
-
- if res['content'] is None:
- self.sf.info("No results found for " + qry)
- return None
-
- try:
- data = json.loads(res['content'])
- except Exception as e:
- self.sf.debug("Error processing JSON response.")
- return None
-
- status = data.get('status')
- if not status == 'found':
- self.sf.info("No results found for " + qry)
- return None
-
- if 'result' not in data:
- self.sf.info("No results found for " + qry)
- return None
-
- if 'summary' not in data['result']:
- self.sf.info("No results found for " + qry)
- return None
-
- if qry not in data['result']['summary']:
- self.sf.info("No results found for " + qry)
- return None
-
- items = data['result']['summary'][qry].get('items')
+ items = self.query('summary', qry, limit)
if not items:
self.sf.debug("No results found for " + qry)
@@ -298,12 +213,14 @@ class sfp_spyonweb(SpiderFootPlugin):
return None
google_adsense = data.get('adsense')
+
if google_adsense:
for r in google_adsense.keys():
evt = SpiderFootEvent("WEB_ANALYTICS_ID", "Google AdSense: " + r, self.__name__, event)
self.notifyListeners(evt)
google_analytics = data.get('analytics')
+
if google_analytics:
for r in google_analytics.keys():
evt = SpiderFootEvent("WEB_ANALYTICS_ID", "Google Analytics: " + r, self.__name__, event)