Sugree

Main menu

  • About
    • Self
  • Project
    • Barcode Generator / Reader
    • GT200
    • GeoPin
    • Mitter
    • Red Mob
    • Thairath Breaking News
    • Twitter Android SDK
    • jibjib
    • microblog-purple
    • English
    • Thai

Twitter to NokNok

Submitted by sugree on Tue, 07/08/2008 - 01:04
  • NokNok
  • Python
  • Social Network
  • Twitter
  • Thai

ช่วงนี้กำลังพยายามปั้น NokNok ให้เป็นดาว อย่างน้อยมันก็รับส่ง SMS ในไทยได้โดยตรง แต่ก็มีปัญหาอยู่บ้าง คือมันไม่มี API ไม่มีไคลเอนต์ ไม่มี IM อีกอย่าง ผมก็ฝั่งตัวเองอยู่ใน Twitter จนไม่อยากไปไหน โดยเฉพาะอย่างยิ่งบริการที่เป็นไทยล้วน เพื่อคนไทยโดยเฉพาะ มันแคบไปนิด เมื่อเป็นเช่นนี้ก็เลยต้องลำบากเขียนโปรแกรมมาช่วยอำนวยความสะดวกกันหน่อย

ไม่ต้องเกริ่นยาว ดูโค้ดดีกว่า

#!/usr/bin/env python
 
from urllib import urlencode
from urllib2 import urlopen, HTTPCookieProcessor, build_opener, install_opener, Request
import simplejson
 
cookie_processor = HTTPCookieProcessor()
opener = build_opener(cookie_processor)
install_opener(opener)
 
user_agent = 'noknok'
 
def user_timeline(u, since_id=0):
    url = 'http://twitter.com/statuses/user_timeline/%s.json' % u
    data = {}
    if since_id > 0:
        data['since_id'] = since_id
    req = Request(url,
                  urlencode(data),
                  {'User-Agent': user_agent})
    fd = urlopen(req)
    results = simplejson.loads(fd.read())
    return filter(lambda x: x['id'] > since_id, results)
 
def summize(q, since_id=0):
    url = 'http://summize.com/search.json'
    req = Request(url,
                  urlencode({'q': q, 'since_id': since_id}),
                  {'User-Agent': user_agent})
    fd = urlopen(req)
    results = simplejson.loads(fd.read())['results']
    return results
 
class NokNok:
 
    url = 'http://www.noknok.in.th/'
 
    def __init__(self, username, password):
        self.username = username
        self.password = password
 
    def home(self):
        url = self.url+'myhome'
        fd = urlopen(url)
        fd.read()
 
    def login(self):
        #print 'logging in'
        url = self.url+'login/'
        fd = urlopen(url, urlencode({'username': self.username,
                                     'password': self.password,
                                     'remember_me': '1'}))
        content = fd.read()
        if fd.code != 200:
            print code, content
        #print fd.headers
        #print content
 
    def post(self, msg):
        #print 'posting %s' % msg
        url = self.url+'message/add'
        req = Request(url, urlencode({'msg_update': msg.encode('utf-8')}), {'Referer': self.url+'myhome'})
        fd = urlopen(req)
        content = fd.read()
        #print fd.headers
        #print content
        if fd.code != 200:
            print code, content
 
def runonce(nok, q, query, since_id=0):
    try:
        results = query(q, since_id=since_id)
    except Exception, why:
        print why
        results = []
    results.reverse()
    for item in results:
        nok.post(item['text'])
        since_id = item['id']
    return since_id
 
def run(nok, q, query):
    import time
 
    since_id = query(q)[0]['id']
    while 1:
        since_id = runonce(nok, q, query, since_id)
        time.sleep(30)
 
if __name__ == '__main__':
    import getpass
 
    source = raw_input('twitter user: ')
    username = raw_input('noknok user: ')
    password = getpass.getpass('noknok password: ')
    nok = NokNok(username, password)
    nok.login()
    run(nok, source, user_timeline)

มันจะอ่านจาก user timeline ของ Twitter แล้วส่งไปที่ noknok ตรงไปตรงมา ถ้าเรียก run() มันจะรันไปเรื่อย ถ้าเปลี่ยนเป็น runonce() จะทำครั้งเดียว เผื่อว่าอยากจะเอาไปรันใน cron ถ้าต้องการอะไรที่ซับซ้อนก็เปลี่ยน user_timeline ตอนเรียก run() เป็น summize เช่น

    run(nok, source, summize)

แล้วใส่ query ที่ twitter user บางครั้งอาจจะช้าซักหน่อย แต่เชื่อถือได้

ปล. ผมไม่เกี่ยวข้องใดๆ กับ Twitter และ NokNok โค้ดข้างบนเขียนด้วยอารมณ์ชั่ววูบ อย่าคาดหวังอะไรมากนัก แก้เองได้ก็แก้ ตามสบายครับ

Roti (alpha) thinks you may like these:
  • (no links yet)
  • Login to post comments
by EscRiBiTioNiSt® (not verified) on Tue, 07/08/2008 - 11:31 #355

อยากทำบ้าง แต่ทำไม่เป็น technical เกินความเข้าใจ - -"

  • Login to post comments
by Teddy-boy (not verified) on Wed, 07/09/2008 - 23:39 #356

พี่สุกรีใช้ภาษาอะไรเขียนขึ้นมาครับ Cอ่ะป่าว

  • Login to post comments
by sugree on Wed, 07/09/2008 - 23:56 #357

Python ดิ ไม่มีปีกกาซักอัน

  • Login to post comments

Popular Tags

Android BarCamp BarCampBangkok Book Django Drupal Education Family Google Hardware Hardy Health Interview Intrepid Java jibjib Joke Life microblog-purple OpenOffice.org OpenSource Podcast Python Review SoC Social Network Trip Twitter Ubuntu Weekly Suki
more tags

Powered By

debian
apache
drupal
drupal.in.th
openfreehost
inox
feedburner

© 2007-2008 Sugree Phatanapherom. Creative Commons [Feed Content / Comment]
RoopleTheme