new: [client] Helper for asns_global_ranking

pull/12/head
Raphaël Vinot 2019-07-22 16:28:58 +02:00
parent c1351fc980
commit 331bdf499c
2 changed files with 29 additions and 9 deletions

View File

@ -16,10 +16,20 @@ from datetime import date, timedelta
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Run a query against BGP Ranking')
parser.add_argument('--url', type=str, help='URL of the instance.')
group = parser.add_mutually_exclusive_group(required=True)
parser.add_argument('--date', default=date.today().isoformat(), help='Date of the dataset required')
sub_parsers = parser.add_subparsers(title='Available commands')
index_query = sub_parsers.add_parser('index')
index_query.add_argument('--limit', default=100, help='Max number of ASN to get')
index_query.add_argument('--family', default='v4', help='v4 or v6')
index_query.set_defaults(which='index')
simple_query = sub_parsers.add_parser('simple')
group = simple_query.add_mutually_exclusive_group(required=True)
group.add_argument('--asn', help='ASN to lookup')
group.add_argument('--ip', help='IP to lookup')
simple_query.set_defaults(which='simple')
args = parser.parse_args()
@ -30,13 +40,16 @@ if __name__ == '__main__':
bgpranking = BGPRanking()
ipasn = IPASNHistory()
if args.ip:
response = ipasn.query(args.ip)
print(json.dumps(response, indent=2))
if 'response' in response and response['response']:
asn = response['response'][list(response['response'].keys())[0]]['asn']
else:
asn = args.asn
if args.which == 'simple':
if args.ip:
response = ipasn.query(args.ip)
print(json.dumps(response, indent=2))
if 'response' in response and response['response']:
asn = response['response'][list(response['response'].keys())[0]]['asn']
else:
asn = args.asn
response = bgpranking.query(asn, date=(date.today() - timedelta(1)).isoformat())
response = bgpranking.query(asn, date=(date.today() - timedelta(1)).isoformat())
elif args.which == 'index':
response = bgpranking.asns_global_ranking(address_family=args.family, limit=args.limit, date=args.date)
print(json.dumps(response, indent=2))

View File

@ -10,6 +10,7 @@ from typing import Union
import requests
from urllib.parse import urljoin
from datetime import date
class BGPRanking():
@ -40,3 +41,9 @@ class BGPRanking():
to_query['source'] = source
r = self.session.post(urljoin(self.root_url, '/json/asn'), data=json.dumps(to_query))
return r.json()
def asns_global_ranking(self, date: str=date.today().isoformat(), address_family: str='v4', limit: int=100):
'''Get the top `limit` ASNs, from worse to best'''
to_query = {'date': date, 'ipversion': address_family, 'limit': limit}
r = self.session.post(urljoin(self.root_url, '/json/asns_global_ranking'), data=json.dumps(to_query))
return r.json()