#!/usr/bin/python
# -*- coding: utf-8 -*-


"""
    Micro-blog
    ~~~~~~~~~~

    Microblog client for twitter

"""

import tweepy

from micro_blog_keys import *
import bgpranking

api = None
auth = None

def __prepare():
    global api
    global auth
    auth = tweepy.OAuthHandler(twitter_consumer_key,
            twitter_consumer_secret)
    auth.set_access_token(twitter_access_key, twitter_access_secret)
    api = tweepy.API(auth)

class CustomStreamListener(tweepy.StreamListener):
    def on_status(self, status):
        if status.text.startswith('@bgpranking AS'):
            asn = status.text.strip('@bgpranking AS')
            msg_id = status.id
            sender = status.user.screen_name
            dates_sources = bgpranking.prepare_sources_by_dates(timeframe=5)
            ranks = bgpranking.get_all_ranks_single_asn(asn, dates_sources)
            to_post = '@{user}: {asn}\n'.format(user=sender, asn=asn)
            for date, info in ranks.iteritems():
                to_post += date + ': ' + round(1+info['total'], 4) + '\n'
            to_post += 'http://bgpranking.circl.lu/asn_details?asn=' + asn
            api.update_status(to_post, msg_id)
        else:
            print status.text

    def on_error(self, status_code):
        print >> sys.stderr, 'Encountered error with status code:', status_code
        return True # Don't kill the stream

    def on_timeout(self):
        print >> sys.stderr, 'Timeout...'
        return True # Don't kill the stream


def stream_mentions():
    options = {'secure':True}
    listener = CustomStreamListener()
    s = tweepy.streaming.Stream(auth, listener, **options)
    s.userstream()

if __name__ == '__main__':
    stream_mentions()

@@ -124,16 +124,27 @@
 t tweepy
+%0Aimport IPy
 %0A%0Afrom m
@@ -208,16 +208,81 @@
 h = None
+%0Abase_asn_address = 'http://bgpranking.circl.lu/asn_details?asn='
 %0A%0Adef __
@@ -701,16 +701,135 @@
 ng AS')%0A
+            try:%0A                int(asn)%0A            except:%0A                print status.text%0A                return%0A
         
@@ -1115,24 +1115,79 @@
 r, asn=asn)%0A
+            dates = sorted(ranks.keys(), reverse=True)%0A
             
@@ -1198,36 +1198,53 @@
 date
-,
  in
-fo in ranks.iteritems():
+ dates:%0A                info = ranks%5Bdate%5D
 %0A   
@@ -1271,176 +1271,1320 @@
  += 
+'%7B
 date
- + ': ' + round(1+info%5B'total'%5D, 4) + '%5Cn'%0A            to_post += 'http://bgpranking.circl.lu/asn_details?asn=' + asn%0A            api.update_status(to_post, msg_id)
+%7D: %7Brank%7D%5Cn'.format(date=date,rank=round(1+info%5B'total'%5D, 4))%0A            to_post += base_asn_address + asn%0A            api.update_status(to_post, msg_id)%0A        elif status.text.startswith('@bgpranking IP'):%0A            ip = status.text.strip('@bgpranking IP')%0A            try:%0A                IPy.IP(ip)%0A            except:%0A                print status.text%0A                return%0A            msg_id = status.id%0A            sender = status.user.screen_name%0A            info = bgpranking.get_ip_info(ip, 10)%0A            to_post_short = '@%7Buser%7D: %7Bip%7D: http://bgpranking.circl.lu/ip_lookup?ip=%7Bip%7D'.format(user=sender, ip=ip)%0A            if len(info%5B'history'%5D) %3E 0:%0A                latest_data = info%5B'history'%5D%5B0%5D%0A                template = '%5Cn%7Basn%7D - %7Bblock%7D: %7Bbase_asn_url%7D%7Basn%7D;ip_details=%7Bblock%7D%5Cn%7Bdescr%7D'%0A                descr = latest_data%5B'descriptions'%5D%5B0%5D%5B1%5D%0A                if len(descr) %3E 40:%0A                    descr = 'Too Long for Twitter.'%0A                to_post = to_post_short + template.format(asn=latest_data%5B'asn'%5D,%0A                        base_asn_url=base_asn_address,%0A                        block=latest_data%5B'block'%5D, descr=descr)%0A            try:%0A                api.update_status(to_post, msg_id)%0A            except:%0A                api.update_status(to_post_short, msg_id)%0A
 %0A   
@@ -3098,16 +3098,32 @@
 ain__':%0A
+    __prepare()%0A
     stre

update twitter bot accordingly to the current version on the server