Sugree

skip to navigation

Tweet to Text

Sometimes you might want to keep your old tweet in your blog forever. Publish the fruitful conversation or debate in your blog everlasting long. There is no easy way yet as far as I know. But it is possible to use Twitter Search. @markpeak asked me for help. Below script is my help.

#!/usr/bin/env python
 
from urllib import urlencode
from urllib2 import urlopen, HTTPCookieProcessor, build_opener, install_opener, Request
from optparse import OptionParser
import time
from datetime import datetime
 
import simplejson
 
cookie_processor = HTTPCookieProcessor()
opener = build_opener(cookie_processor)
install_opener(opener)
 
api_url = 'http://search.twitter.com/search.json'
results_per_page = 100
verbose = False
 
def search(q, data=None):
    if not data:
        data = urlencode({'q': q,
                          'rpp': results_per_page})
    if verbose:
        print data
    req = Request(api_url, data)
    fd = urlopen(req)
    results = simplejson.loads(fd.read())
    return results
 
def unescape(t):
    t = t.replace('"', '"')
    t = t.replace('&', '&')
    return t
 
def format_tweet(t):
    created_at = datetime.fromtimestamp(time.mktime(
                 time.strptime(t['created_at'][:-6], '%a, %d %b %Y %H:%M:%S'))-
                 time.timezone)
    return '(%s) %s: %s' % (created_at.strftime('%H:%M'),
                            t['from_user'],
                            unescape(t['text']))
 
class App:
    def __init__(self):
        parser = OptionParser()
        parser.add_option('-v', '--verbose',
                          default=False, action='store_true', dest='verbose',
                          help='verbose')
        parser.add_option('-r', '--rpp',
                          default=20, type='int', dest='rpp',
                          help='results per page')
        parser.add_option('-p', '--pages',
                          default=4, type='int', dest='pages',
                          help='max pages')
 
        self.options, self.args = parser.parse_args()
 
        global verbose, results_per_page
        verbose = self.options.verbose
        results_per_page = self.options.rpp
 
    def run(self):
        history = []
        query = ' '.join(self.args)
        results = search(query)
        for i in results['results']:
            history.append(i)
 
        for p in range(self.options.pages-1):
            if 'next_page' not in results:
                break
            next_page = results['next_page']
            results = search(query, next_page[1:])
            for i in results['results']:
                history.append(i)
 
        history.reverse()
        for i in history:
            print format_tweet(i)
 
        if verbose:
            print len(history)
 
if __name__ == '__main__':
    app = App()
    app.run()

The usage is very straightforward. Simply call above script with query you want. There are 3 options.

For example:

sugree@sugree-laptop:twittertrack$ python track.py -r 20 -p 1 from:nytonkla OR from:hohoteam
(23:34) nytonkla: กลับบ้านมาแล้ว อย่างง่วง
(00:24) hohoteam: @markpeak ไปสิ FOWA อิอิ
(00:40) nytonkla: นอนแล้วนะ ง่วงเหนื่อย
(00:43) hohoteam: manager manager manager
(00:51) hohoteam: register 1 domain success
(01:19) hohoteam: @kengggg อย่าลืมเอา clips งาน ไปแปะใน Barcamp นะเฟ้ย ส่งไปให้แล้ว เดี๋ยวจะช้าเกิน
(01:25) hohoteam: ปะทะกันแล้ว
(09:47) hohoteam: chrome VS firefox
(09:49) hohoteam: welcome to the real world. good luck firefox. good bye chrome. long live IE.
(10:11) hohoteam: @dtinth not sure. i cannot access to their site. read review only.
(10:14) hohoteam: เบอร์บาตอฟ 30.75 ล้านปอนด์ + แคมป์เบลล์ บ้าป่าวว่ะ ไม่คุ้มเลย เอาเด็กไปแลกทำไมเนี่ย เงินก็มี
(10:15) hohoteam: ฮ่วย โรบินโญ่มาแมนซิตี้ 32.5 ล้านปอนด์ ตัดหน้าเชลซี อะไรกันเนี่ย
(10:20) hohoteam: พี่แม้วขายแมนซิตี้ไปแว้ว นกรู้???
(10:20) hohoteam: ปลาดุก ซาฮา ไปท๊อฟฟี่แล้ว ขอให้โชคดีนะ
(10:20) hohoteam: @NaiOhm ตอนนั้นผ่าไส้ติ่งอยู่ครับ ไปไม่ได้
(10:37) hohoteam: ลุงหมักด่านักข่าวว่าอย่าพูดแซง รอให้จบก่อน หงิดไปเลย
(10:38) hohoteam: นักข่าวถามได้ซังกะบ๊วยมาก
(10:41) hohoteam: @NaiOhm ได้ครับ email มาที่ hoho AT duocore.tv
(10:44) hohoteam: อ่ะ แค่ให้ยืมตัว โอเคๆ
(11:01) nytonkla: อ้าวเวร.. กกต. สรุปยุบพรรคพลังปปช.

Note to @markpeak: The correct argument for you is -r 100 -p 20.

Submitted by sugree on Tue, 09/02/2008 - 10:42

Post new comment

The content of this field is kept private and will not be shown publicly.
+

Main menu

+++++++++

Popular Tags

debian
apache
drupal
drupal.in.th
feedburner

++++++++++++++++++++++