acasown/ripe/__init__.py

24 lines
1.1 KiB
Python
Raw Permalink Normal View History

2018-11-08 12:56:36 +01:00
import requests
import logging
class Ripe:
def __init__(self):
self.ranges = []
def search(self, target):
r = requests.get("https://apps.db.ripe.net/db-web-ui/api/rest/fulltextsearch/select?facet=true&format=xml&hl=true&q=(" + target +")+AND+(object-type:inetnum)&start=0&wt=json", headers={"Accept": "application/json"})
data = r.json()
2018-11-08 15:55:24 +01:00
self.parse(data)
2018-11-08 12:56:36 +01:00
total = data['result']['numFound']
2018-11-08 15:55:24 +01:00
for i in range(10, total, 10):
2018-11-08 12:56:36 +01:00
r = requests.get("https://apps.db.ripe.net/db-web-ui/api/rest/fulltextsearch/select?facet=true&format=xml&hl=true&q=(" + target +")+AND+(object-type:inetnum)&start=" + str(i) + "&wt=json", headers={"Accept": "application/json"})
self.parse(r.json())
return self.ranges
def parse(self, data):
for entry in data['result']['docs']:
for field in entry['doc']['strs']:
if field['str']['name'] == 'inetnum':
self.ranges.append({'start': field['str']['value'].split('-')[0].strip(), 'end': field['str']['value'].split('-')[1].strip()})