Skip to content

Commit

Permalink
42qu
Browse files Browse the repository at this point in the history
  • Loading branch information
ifduyue committed Aug 28, 2011
1 parent e76f27a commit 6e9b868
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 1 deletion.
63 changes: 63 additions & 0 deletions _42qu/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#coding: utf8
import sys
sys.path.insert(0, '..')
from bc import BC
import re
import urllib
import pycurl

class _42qu(BC):

def __init__(self, username, password):
self.username = username
self.password = password
self.cookie_file = username + '.42qu_cookie'
BC.__init__(self)
self.reset()

def login(self):
b, c = self.reset()
c.setopt(pycurl.URL, "http://42qu.com/auth/login")
c.setopt(pycurl.REFERER, 'http://42qu.com/')
c.setopt(pycurl.COOKIEJAR, self.cookie_file)
c.perform()
data = b.getvalue()
xsrf = re.search('''name="_xsrf".*?value="(.*?)"''', data).group(1)
self.xsrf = xsrf

b, c = self.reset()
c.setopt(pycurl.COOKIEJAR, self.cookie_file)
c.setopt(pycurl.URL, "http://42qu.com/auth/login")
c.setopt(pycurl.POST, True)
c.setopt(pycurl.POSTFIELDS, urllib.urlencode({
'_xsrf': xsrf,
'mail': self.username,
'password': self.password,
}))
try:
c.perform()
except pycurl.error, e:
if e.args[0] != 47: raise
self.eurl = c.getinfo(pycurl.EFFECTIVE_URL)
self.eurl = self.eurl.replace('42qu.com//','')
#self.eurl = self.eurl.replace('/live', '')
return b.getvalue()

def update(self, status):
b, c = self.reset()
c.setopt(pycurl.COOKIEJAR, self.cookie_file)
c.setopt(pycurl.URL, self.eurl.replace('/live', '/po/word'))
c.setopt(pycurl.REFERER, self.eurl)
c.setopt(pycurl.POST, True)
c.setopt(pycurl.POSTFIELDS, urllib.urlencode({
'txt': status,
'_xsrf' : self.xsrf,
}))
c.perform()
return b.getvalue()


def pub2_42qu(username, password, status):
o = _42qu(username, password)
o.login()
o.update(status)
7 changes: 7 additions & 0 deletions bc.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,21 @@ class BC:
def __init__(self):
self.c = pycurl.Curl()
self.b = StringIO()
self.h = ''

def reset(self):
b, c = self.b, self.c
self.h = ''
b.truncate(0)
c.reset()
c.setopt(pycurl.WRITEFUNCTION, b.write)
c.setopt(pycurl.CONNECTTIMEOUT, 5)
c.setopt(pycurl.TIMEOUT, 10)
c.setopt(pycurl.USERAGENT, 'Opera/9.60')
c.setopt(pycurl.FOLLOWLOCATION, True)
c.setopt(pycurl.MAXREDIRS, 5)
c.setopt(pycurl.HEADERFUNCTION, self.headerfunction)
return b, c

def headerfunction(self, h):
self.h += h
2 changes: 2 additions & 0 deletions conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@
qq_passwd = ''
fanfou_user = ''
fanfou_passwd = ''
_42qu_user = ''
_42qu_passwd = ''
10 changes: 9 additions & 1 deletion pub2all.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def pub2all(status):
from facebook import pub2facebook
from qq import pub2qq
from fanfou import pub2fanfou
from _42qu import pub2_42qu
import conf

flag = False
Expand Down Expand Up @@ -55,7 +56,14 @@ def pub2all(status):
flag = True
except Exception, e:
log('pub2fanfou error: %s' % str(e))


if conf._42qu_user and conf._42qu_passwd:
try:
pub2_42qu(conf._42qu_user, conf._42qu_passwd, status)
flag = True
except Exception, e:
log('pub2_42qu error: %s' % str(e))

return flag


Expand Down

0 comments on commit 6e9b868

Please sign in to comment.