Skip to content

Instantly share code, notes, and snippets.

@RitzoneX
Created Jun 28, 2015
Embed
What would you like to do?
#!/usr/bin/python
# -*- coding: utf-8 -*-
import requests
import re
import time
# import util
# import proxy
from cStringIO import StringIO
from PIL import Image
import pytesseract
def sleep():
print 'sleep 30s'
time.sleep(30)
print 'finish sleep'
def verify(verifyimg):
img = Image.open(StringIO(verifyimg))
vcode = pytesseract.image_to_string(img, config='digits')
if re.match(r'^\d{4}$', vcode):
return vcode
else:
import random
return random.randint(1000, 9999)
def search(pattern):
m = re.search(pattern, r.content)
try:
return m.group(1)
except Exception, e:
return m.group()
def session(referer):
s = requests.Session()
s.headers['Referer'] = referer
s.headers['User-Agent'] = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.124 Safari/537.36'
# s.proxies['http'] = 'http://' + proxy.random_proxy()
return s
def yunfile(referer, url):
try:
global s, r
s = session(referer)
r = s.get(url, timeout=10)
url = page1()
r = s.get(url)
if r.url != url:
r = s.get(page1())
page2()
except Exception, e:
print e
def page1():
s.headers['Referer'] = r.url
print 'Page1: ' + r.url
# with open('1.html', 'wb') as f:
# f.write(r.content)
# counter
s.get(search(r'http://www.yunfile.com//counter.jsp[^"]*'))
path = '/'.join(r.url.split('/')[:3])
url = path + eval(search(r'"/verifyimg.*?html"'))
print url
verifyimg = s.get(url).content
sleep()
url = path + search(r'id="downpage_link.*href="(.*).html')
url += '/%s.html' % verify(verifyimg)
return url
def page2():
s.headers['Referer'] = r.url
print 'Page2: ' + r.url
# with open('2.html', 'wb') as f:
# f.write(r.content)
# ckcounter
cookies = {'lastDownTime': search(r'lastDownTime", "(\d+)"')}
print cookies
s.get(search(r'http://www.yunfile.com/ckcounter.jsp[^"&]*'), cookies=cookies)
data = {
'module': 'fileService',
'action': 'downfile',
'userId': search(r'name="userId" value="(.*?)"'),
'fileId': search(r'fileId.value = "(.*?)"'),
'vid': search(r'vericode = "(.*?)"'),
'vid1': search(r'name="vid1" value="(.*?)"'),
'md5': search(r'name="md5" value="(.*?)"')
}
print data
url = search(r'id="d_down_from" action="(.*?)"')
print 'downurl: ' + url
print '下载中。。'
res = s.post(url, data=data, cookies=cookies)
print '下载完成'
# 下载文件为res.content
s = None
r = ''
if __name__ == '__main__':
yunfile('http://p1.yunfile.com/fs/3R5i0t4zdo6n3e9X0/', 'http://p1.yunfile.com/fs/3R5i0t4zdo6n3e9X0/')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment