Skip to content

Commit

Permalink
Add client API, Readme and move server code.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafiot committed Feb 12, 2013
1 parent 3165456 commit 0f8a4f2
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 12 deletions.
16 changes: 16 additions & 0 deletions README
@@ -0,0 +1,16 @@
This project aims to provide a simple way to get the description of an ASN, and
to see the changes over the time.

We use the data provided by cidr-report.org:
http://www.cidr-report.org/as2.0/autnums.html

Server
======

Fetch ASNs descriptions from cidr-report.org, parse the HTML file and populate
a redis database.

Client
======

Query this database to get the description(s) (latest or all).
1 change: 1 addition & 0 deletions client/README.md
@@ -0,0 +1 @@
Query a redis database to access to the ASNs descriptions.
4 changes: 4 additions & 0 deletions client/asnhistory/__init__.py
@@ -0,0 +1,4 @@
import api
api.__prepare()

from api import *
63 changes: 63 additions & 0 deletions client/asnhistory/api.py
@@ -0,0 +1,63 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-

import redis
import dateutil.parser

redis_host = '127.0.0.1'
redis_db = 0
redis_port = 6389

r = None

def __prepare():
global r
if r is None:
r = redis.Redis(host = redis_host, port=redis_port, db=redis_db)


def get_all_descriptions(asn):
"""
Get all the descritpiosn available in the database for this ASN.
Most recent first.
[
(datetime.datetime(), 'description 1'),
(datetime.datetime(), 'description 2'),
...
]
"""
all_descrs = r.hgetall(asn)
dates = sorted(all_descrs.keys(), reverse=True)
to_return = []
for date in dates:
d = dateutil.parser.parse(date)
to_return.append((d, all_descrs[date]))
return to_return


def get_last_description(asn):
"""
Get only the most recent description.
"""
all_descrs = r.hgetall(asn)
if len(all_descrs) == 0:
return None
dates = sorted(all_descrs.keys())
return all_descrs[dates[-1]]

def get_last_update():
last_update = r.get('last_update')
if last_update is not None:
return dateutil.parser.parse(last_update)
return None

def get_all_updates():
all_updates = sorted(r.smembers('all_timestamps'), reverse=True)
if len(all_updates) == 0:
return None
to_return = []
for u in all_updates:
to_return.append(dateutil.parser.parse(u))
return to_return

23 changes: 23 additions & 0 deletions client/setup.py
@@ -0,0 +1,23 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
from distutils.core import setup

try:
from distutils.command.build_py import build_py_2to3 as build_py
except ImportError:
from distutils.command.build_py import build_py

setup(
name='asnhistory',
version='1.0',
description='Query a redis database to access to the ASNs descriptions.',
url='https://github.com/Rafiot/ASN-Description-History',
author='Raphaël Vinot',
author_email='raphael.vinot@circl.lu',
maintainer='Raphaël Vinot',
maintainer_email='raphael.vinot@circl.lu',
packages=['asnhistory'],
cmdclass = {'build_py': build_py},
license='GNU GPLv3',
long_description=open('README.md').read(),
)
6 changes: 0 additions & 6 deletions constraint.py

This file was deleted.

14 changes: 8 additions & 6 deletions asn_history.py → server/asn_history.py
@@ -1,4 +1,5 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-

import argparse
import dateutil.parser
Expand All @@ -10,9 +11,11 @@
import time
import urllib

import constraint as c
sleep_timer = 3600
redis_host = '127.0.0.1'
redis_db = 0
redis_port = 6389

sleep_timer = 36000

def __prepare(directory):
temp_dir = os.path.join(directory, 'temp')
Expand Down Expand Up @@ -69,13 +72,13 @@ def parse(directory):
args = parser.parse_args()
__prepare(args.directory)

publisher.port = c.redis_port
publisher.port = redis_port
publisher.channel = 'ASN_History'
time.sleep(5)
publisher.info('Importer started.')
while True:
for timestamp, data in parse(args.directory):
r = redis.Redis(host = c.redis_host, port=c.redis_port, db=c.redis_db)
r = redis.Redis(host = redis_host, port=redis_port, db=redis_db)

last_update = r.get('last_update')
if last_update > timestamp:
Expand All @@ -98,8 +101,7 @@ def parse(directory):
publisher.debug('New asn: {asn}'.format(asn = asn))
new_asns += 1
else:
dates = all_descrs.keys()
dates.sort()
dates = sorted(all_descrs.keys())
last_descr = all_descrs[dates[-1]]
if descr != last_descr:
p.hset(asn, timestamp, descr)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 0f8a4f2

Please sign in to comment.