Monthly Archive for enero, 2010

Machu Picchu

Machu Picchu
Con Nacho y Gali en Machu Picchu, a fines de diciembre de 2009

Identificando posibles “drops” de usuarios de Twitter, con Python

Hace un par de días se supo que Twitter liberará aquellos nombres de usuario que no hayan registrado actividad durante 6 meses o más.

Les dejo una herramienta para obtener los días de inactividad de un usuario a través de su último status publicado. Esta clase puede ser utilizada, por ejemplo, para recorrer listados de palabras en búsqueda de nombres valiosos.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import twitter
import sys
import urllib2
from datetime import datetime
 
class UnknownUser(Exception):
    pass
 
class UnknownStatus(Exception):
    pass
 
class TwitterExpire(object):
    def __init__(self):
        self.__t = twitter.Api()
 
    def verify(self, username):
        try:
            user = self.__t.GetUser(user=username)
        except urllib2.HTTPError:
            raise UnknownUser
        else:
            status = user.GetStatus()
            if status:
                timestamp = status.GetCreatedAtInSeconds()
                d = datetime.fromtimestamp(timestamp)
                td = datetime.now() - d
                return td
            else:
                raise UnknownStatus
 
def main(username):
    te = TwitterExpire()
    try:
        result = te.verify(username)
    except UnknownUser:
        print u"User '%s' doesn't exist" % username
    except UnknownStatus:
        print u"Couldn't find any status for '%s'" % username
    else:
        print result
 
if __name__ == '__main__':
    if len(sys.argv) >= 2:
        main(username=sys.argv[1])

Algunos resultados:

mahadeva@blue:~$ python2.5 last_status.py patito
258 days, 11:16:49.425212
mahadeva@blue:~$ python2.5 last_status.py patricio
Couldn't find any status for 'patricio'
mahadeva@blue:~$ python2.5 last_status.py usuarioinexistente
User 'usuarioinexistente' doesn't exist
mahadeva@blue:~$ python2.5 last_status.py shitmydadsays
1 day, 22:35:44.434214



This work by Patricio Molina is licensed under a Creative Commons Attribution-ShareAlike 2.5 Argentina.