Skip to content

Instantly share code, notes, and snippets.

@tracyhatemice
Created January 31, 2016 07:07
Show Gist options
  • Save tracyhatemice/65c9bb6c5474cf493301 to your computer and use it in GitHub Desktop.
Save tracyhatemice/65c9bb6c5474cf493301 to your computer and use it in GitHub Desktop.
pyFetion
# -*- coding: utf-8 -*-
'''
Created on 2013-1-31
@author: lwx
'''
import cookielib, urllib2, urllib, sys, time, json, os
class fetion:
def __init__(self, phone, passwd, Rand = None):
self.cj = cookielib.LWPCookieJar()
self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cj))
self.opener.addheaders = [('User-agent','Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36')]
urllib2.install_opener(self.opener)
self.host = 'http://f.10086.cn'
self.phone = phone
self.passwd = passwd
self.Rand = Rand
self.check_login()
def userinfo(self):
url = self.host + '/im5/user/selfInfo.action?t=' + str(long(time.time()*1000))
data = {'t':tm}
request = urllib2.Request(url, urllib.urlencode(data))
response = urllib2.urlopen(request)
return response
def check_login(self):
try:
ret = json.loads(self.userinfo().read())
idUser = ret.get('userinfo', {}).get('idUser', '')
except:
idUser = False
if not idUser:
self.new_passcode()
else:
print('-- fetion login Success [cookies]')
thread.start_new_thread(self.keep_online, ())
return idUser
def keep_online(self):
while 1:
time.sleep(120)
self.userinfo()
def new_passcode(self):
global tm
tm = str(long(time.time()*1000))
url = self.host + "/im5/systemimage/verifycode{0}.png?tp=im5&t={0}".format(tm)
request = urllib2.Request(url)
im = urllib2.urlopen(request)
if self.Rand:
from utils.Rand import APIClient
global Yunclient
global Rand
Yunclient = APIClient(self.log, self.Rand.username, self.Rand.passwd, self.Rand.host)
Yunclient.userinfo()
Rand = Yunclient.codevalue(im.read())
rand_code = Rand[0]
else:
imf = 'rand.jpg'
with open(imf, 'wb') as fw:
fw.write(im.read())
try:
os.startfile(imf)
except:
import Image
im = Image.open(imf)
im.show()
rand_code = raw_input('Fetion Rand Code:')
self.login(rand_code)
def login(self, rand_code):
url = self.host + '/im5/login/loginHtml5.action?t=' + tm
data = {
'm':self.phone
,'pass':self.passwd
,'captchaCode':rand_code
,'checkCodeKey':'null'
}
request = urllib2.Request(url, urllib.urlencode(data))
response = urllib2.urlopen(request)
ret = json.loads(response.read())
if ret['tip'] == '':
print('-- fetion login Success [requests]')
else:
print(ret['tip'])
try:
Yunclient.senderror(*Rand)
except Exception, e:
pass
self.new_passcode()
def __get_id_by_tel(self, toPhone):
url = self.host + "/im5/index/searchFriendsByQueryKey.action"
data = {
"queryKey":toPhone
}
request = urllib2.Request(url, urllib.urlencode(data))
response = json.loads(urllib2.urlopen(request).read())
ret = response.get("contacts",[{}])[0].get("idContact", False)
return ret
def send_msg(self, toPhones, msg):
url = self.host + "/im5/chat/sendNewMsg.action"
for index, toPhone in enumerate(toPhones):
uid = self.__get_id_by_tel(toPhone)
if uid:
data = {
"touserid":uid
,"msg":msg.encode("utf8")
}
request = urllib2.urlopen(urllib2.Request(url, urllib.urlencode(data)))
reponse = request.read()
ret = json.loads(reponse)
print('-- [%s] %s\t%s' % (toPhone, ret["info"], ret["sendCode"]))
else:
print('-- [%s] 拉取飞信好友信息失败!' % toPhone)
if __name__ == "__main__":
'''
目前只能发送给飞信好友。
'''
f = fetion(phone = test, passwd = test001)
f.send_msg(toPhones = [phone1, phone2], msg = '这是一条测试短信')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment