Created
February 25, 2013 14:08
-
-
Save Wizmann/5030001 to your computer and use it in GitHub Desktop.
人人网发状态机
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#coding=utf-8 | |
import urllib | |
import urllib2 | |
import cookielib | |
import httplib | |
import json | |
import re | |
import time | |
import datetime | |
import os | |
import sys | |
import socket | |
import sqlite3 | |
import logging | |
def get_log(): | |
log = logging.getLogger('renren') | |
handler = logging.FileHandler('renren_log.txt','a') | |
fmt = logging.Formatter("%(levelname)-8s %(asctime)-15s [%(filename)s,%(lineno)d] %(message)s") | |
handler.setFormatter(fmt) | |
log.addHandler(handler) | |
log.setLevel(logging.DEBUG) | |
return log | |
def getCookieToken(): | |
sql_query='SELECT value FROM cookies WHERE host_key=\'.renren.com\' and name=\'t\' and length(value)=33;' | |
sql_path=os.path.join(os.path.expanduser('~'),'.config/google-chrome/Default/Cookies') | |
conn=sqlite3.connect(sql_path) | |
cursor=conn.cursor() | |
cursor.execute(sql_query) | |
return cursor.fetchall()[0][0] | |
def status(status,token,rtk,myid,cookie): | |
#发布状态 | |
params = {'content':status,'hostid':myid,'requestToken':token,'channel':'renren','_rtk':rtk} | |
# 进行编码,并请求 | |
req = urllib2.Request( | |
'http://shell.renren.com/'+myid+'/status', | |
urllib.urlencode(params) | |
) | |
req.add_header('Cookie', cookie) | |
content = urllib2.urlopen(req,timeout=10).read() | |
return content | |
def show(now_time): | |
logger=get_log() | |
try: | |
cookie = "t="+getCookieToken()+";" | |
except: | |
logger.info("Get Cookie Error") | |
exit(0) | |
try: | |
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookielib.CookieJar())) | |
urllib2.install_opener(opener) | |
req = urllib2.Request("http://friend.renren.com/myfriendlistx.do#item_0") | |
req.add_header('Cookie', cookie) | |
content = urllib2.urlopen(req).read() | |
myid = "330844449" | |
token = re.findall('get_check:\'[\d-]*\'?',content)[0].split(':')[1].strip('\'') | |
rtk = re.findall('get_check_x:\'.*?\'',content)[0].split(':')[1].strip('\'') | |
except: | |
logger.info("Get Page Error!") | |
exit(0) | |
try: | |
logger.debug(status(now_time,token,rtk,myid,cookie)) | |
except Exception,e: | |
print e | |
logger.info("Send Status Error!") | |
exit(0) | |
logger.info("OK") | |
def get_now_time(): | |
return time.strftime('%Y-%m-%d %H:%M',time.localtime(time.time())) | |
def get_spring(): | |
return '距离2013年的春分,还有%d小时!' % int((datetime.datetime(2013,3,20,19)-datetime.datetime.now()).total_seconds()/60/60) | |
def get_weather(cityid): | |
city_id_dict={'北京':'101010100','天津':'101030100'} | |
cityid=city_id_dict[cityid] | |
conn = httplib.HTTPConnection("www.weather.com.cn") | |
conn.request("GET", "/data/sk/{0}.html".format(cityid)) | |
weather_json=conn.getresponse().read() | |
conn.close() | |
weather_dict=json.loads(weather_json)['weatherinfo'] | |
for key,value in weather_dict.items(): | |
weather_dict[key]=value.encode("utf-8") | |
return '%(city)s实时天气:气温%(temp)s℃ %(WD)s%(WS)s' % weather_dict | |
if(__name__=='__main__'): | |
show(get_now_time()+" (走你) "+get_spring()+" (bs) "+get_weather('北京')+" "+get_weather('天津')+" (口罩) 大家注意保暖,饱暖思淫欲!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment