Skip to content

Commit

Permalink
a dirty implement stores cookies using MozillaCookieJar
Browse files Browse the repository at this point in the history
  • Loading branch information
falconchen committed Sep 2, 2012
1 parent 3723515 commit 98ea921
Showing 1 changed file with 32 additions and 19 deletions.
51 changes: 32 additions & 19 deletions PyWapFetion/Fetion.py
@@ -1,5 +1,6 @@
#coding=utf-8
from cookielib import CookieJar as _CookieJar
from cookielib import MozillaCookieJar
#from cookielib import CookieJar as _CookieJar
from urllib2 import Request, build_opener, HTTPHandler, HTTPCookieProcessor
from urllib import urlencode
import base64
Expand All @@ -21,18 +22,20 @@
__all__ = ['Fetion']


class CookieJar(_CookieJar):
"""http://stackoverflow.com/questions/1023224/how-to-pickle-a-cookiejar"""
def __getstate__(self):
state = self.__dict__.copy()
del state['_cookies_lock']
return state
# class CookieJar(_CookieJar):
# """http://stackoverflow.com/questions/1023224/how-to-pickle-a-cookiejar"""
# def __getstate__(self):
# state = self.__dict__.copy()
# del state['_cookies_lock']
# return state

def __setstate__(self, state):
self.__dict__ = state
self._cookies_lock = threading.RLock()
# def __setstate__(self, state):
# self.__dict__ = state
# self._cookies_lock = threading.RLock()




class Fetion(object):
def __init__(self, mobile, password=None, status='0',
cachefile='Fetion.cache', cookiesfile=''):
Expand All @@ -45,18 +48,28 @@ def __init__(self, mobile, password=None, status='0',
if not cookiesfile:
cookiesfile = '%s.cookies' % mobile

# try:
# with open(cookiesfile, 'rb') as f:
# cookie_processor = load(f)
# except:
# cookie_processor = HTTPCookieProcessor(CookieJar())
cookiejar = MozillaCookieJar(filename=cookiesfile)
try:
with open(cookiesfile, 'rb') as f:
cookie_processor = load(f)
except:
cookie_processor = HTTPCookieProcessor(CookieJar())

f=open(cookiesfile)
except IOError:
f=open(cookiesfile,'w')
f.write(MozillaCookieJar.header)
finally:
f.close()
cookiejar.load(filename=cookiesfile)
cookie_processor = HTTPCookieProcessor(cookiejar)
self.opener = build_opener(cookie_processor,
HTTPHandler)
self.mobile, self.password = mobile, password
if not self.alive():
self._login()
dump(cookie_processor, open(cookiesfile, 'wb'))
cookiejar.save()
#dump(cookie_processor, open(cookiesfile, 'wb'))
self.changestatus(status)

def send2self(self, message, time=None):
Expand All @@ -78,8 +91,8 @@ def addfriend(self, mobile, name='xx'):
{'nickname': name, 'number': phone, 'type': '0'})
return '成功' in htm

def alive(self):
return '心情' in self.open('im/index/indexcenter.action')
def alive(self):
return '正在登录' in self.open('im/index/indexcenter.action')

def deletefriend(self, id):
htm = self.open('im/user/deletefriendsubmit.action?touserid=%s' % id)
Expand Down Expand Up @@ -107,7 +120,7 @@ def _login(self):
page = self.open('/im5/login/loginHtml5.action')
captcha = codekey.findall(page)[0]
img = self.open('/im5/systemimage/verifycode%s.jpeg' % captcha)
open('verifycode.jpeg', 'w').write(img)
open('verifycode.jpeg', 'wb').write(img)
captchacode = raw_input('captchaCode:')
data['captchaCode'] = captchacode
htm = self.open('/im5/login/loginHtml5.action', data)
Expand Down

0 comments on commit 98ea921

Please sign in to comment.