Python QQ空間シミュレーションアップロード画像コード

4383 ワード

# -*- coding: utf-8 -*-
##      


from PIL import Image
import base64
import urllib    
import urllib2
import datetime
print datetime.datetime.now().microsecond

import re


##        ,    filelen filemd5  
def update_pic_to_qq(picpath):
    f=open(picpath,'rb') #          
    ls_f=base64.b64encode(f.read()) #      ,   base64   
    f.close()
    #print ls_f
    img = Image.open(picpath)
    print img.size
    #exit();

    hd_width=img.size[0]
    hd_height=img.size[1]

    url = 'http://up.qzone.com/cgi-bin/upload/cgi_upload_pic_v2'  
      
    user_agent = 'Mozilla/5.0 (Linux; U; Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1'    
    values = {'picture' : ls_f ,    
              'base64' : '1',    
              'hd_height' : hd_height,
               'hd_width' : hd_width,
               'hd_quality' : '96',
               'output_type' : 'json',
               'preupload' : '1',
               'charset' : 'utf-8',
               'output_charset' : 'utf-8',
               'logintype' : 'sid',
               'Exif_CameraMaker' : '',
               'Exif_CameraModel':'',
               'Exif_Time':'',
               'uin':'QQ  ',
               'sid' : 'QQ  SID' }    
      
    headers = { 'User-Agent' : user_agent ,
                'Referer':'http://m.qzone.com/infocenter?g_f=2425'}
    data = urllib.urlencode(values)    
    req = urllib2.Request(url, data, headers)    
    response = urllib2.urlopen(req)    
    the_page = response.read()
    #print the_page
    #print '  :',data
    print '  :',the_page
    return the_page


#       
def update_pic_to_xiangce(filelen,md5):
    url = 'http://up.qzone.com/cgi-bin/upload/cgi_upload_pic_v2'  
  
    user_agent = 'Mozilla/5.0 (iPhone; U; CPU iPhone OS 6_0_1 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8C148 Safari/6533.18.5'    
    values = {'output_type' : 'json' ,    
              'preupload' : '2',    
              'md5' : md5,
               'filelen' : filelen,
               'batchid' : '',
               'currnum' : '0',
               'uploadNum' : '1',
               'uploadtime' : '',
               'uploadtype' : '1',
               'upload_hd' : '1',
               'albumtype' : '7',
               'big_style':'1',
               'op_src':'15001',
              'charset':'utf-8',
              'output_charset':'utf-8',
              'uin':'QQ  ',
              'sid':'QQ  SID',
              'logintype':'sid',
              'mobile_dc':'actiontype%3D2%26subactiontype%3D1%26reserves%3D1%26page_type%3D2%26app_id%3D7003',
              'albumid':'QQ  ID',
              'desc':'       ',
               'platformid':'52',
               'platformsubid' : '11' }    
      
    headers = { 'User-Agent' : user_agent ,
                'Referer':'http://m.qzone.com/infocenter?g_f=2425'}
    data = urllib.urlencode(values)    
    req = urllib2.Request(url, data, headers)    
    response = urllib2.urlopen(req)    
    the_page = response.read()
    #print the_page
    #print '  :',data
    print '  :',the_page
    

##    
def test_main():
    #p=re.compile(r'filelen" : ([0-9]+)')
    #al=p.findall('"filelen" : 20319,')
    #print al[0]

    result=update_pic_to_qq(r'E:\tempImage\p7.jpg')
    p_1=re.compile(r'filelen" : ([0-9]+)')
    p_2=re.compile(r'filemd5" : "([^<>]+)"')
    filelen=p_1.findall(result)
    md5=p_2.findall(result)
    print 'filelen',filelen
    print 'md5',md5

    update_pic_to_xiangce(filelen[0],md5[0])


test_main()