再次修改skydrive-gae获取skydrive外链

与天斗,与地斗,与微软斗,其乐无穷
微软再一次修改了skydrive的文件获取逻辑,上一个版本的skydrive-gae终于失效

仔细分析了新版的文件重定向流程
比如访问URL:http://cid-e29fae4e10288b80.office.live.com/self.aspx/Public/PRMY-0018/PRMY-0018.rar
Response的Header的Location:https://skydrive.live.com/self.aspx/Public/PRMY-0018/PRMY-0018.rar?cid=e29fae4e10288b80&sc=documents

于是浏览器重定向访问:https://skydrive.live.com/self.aspx/Public/PRMY-0018/PRMY-0018.rar?cid=e29fae4e10288b80&sc=documents
Response的Header的Location:https://skydrive.live.com/?cid=e29fae4e10288b80&id=E29FAE4E10288B80%21300&sc=documents

浏览器再次重定向至https://skydrive.live.com/?cid=e29fae4e10288b80&id=E29FAE4E10288B80%21300&sc=documents
重定向到此为止

这个地方是关键:

E29FAE4E10288B80%21300

拿去喂skydrive.live.com/download.aspx就能获取动态的文件地址

http://skydrive.live.com/download.aspx?cid=E29FAE4E10288B80&resid=E29FAE4E10288B80%21300&canary=

于是关键地方就是获取所谓的resid,要害在第二次重定向

温习了下python,现学现用

# -*- coding: utf-8 -*-
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
import urllib2, httplib
import re

def getcid(link):
    pos = link.find('.office.live.com')
    return link[4:pos]

def to_new_type_link(link):
    pos = link.find('.office.live.com')
    cid = link[4:pos]
    uri = link[pos+len('.office.live.com'):]
    new_link = 'skydrive.live.com' + uri + '?cid=' + cid# + '&sc=documents'
    return new_link

def get_real_link(static_url):    
    curl = "http://" + static_url
    request = urllib2.Request(curl)
    opener = urllib2.build_opener()
    response = opener.open(request)
    realurl = response.geturl()
    return realurl

def get_dynamic_download_link(real_url, cid):
    pos = real_url.find(cid) + len(cid) + len('&id=')
    resid = real_url[pos:]
    return 'http://skydrive.live.com/download.aspx?cid=' + cid.upper() + '&resid=' + resid + '&canary='

def replace_html_code(old_link):
    new_link = old_link.replace('\\/','/')
    new_link = new_link.replace('https://','http://')
    return new_link

class MainPage(webapp.RequestHandler):
    def get(self):
        old_type_link = self.request.path[1:]
        cid = getcid(old_type_link)
        new_type_link = to_new_type_link(old_type_link)
        real_link = get_real_link(new_type_link)
        self.response.headers['Content-Type'] = 'text/plain'
        self.redirect(replace_html_code(get_dynamic_download_link(real_link, cid)))
        
class IndexPage(webapp.RequestHandler):
    def get(self):
        self.response.out.write('''<html>
<head>
<title>
skydrie-gae
</title>
<meta content="A Google App Engine application for redirect the skydrive link" name="description">
<meta content="skydrive, skydirve-gae, gae, google app engin," name="keywords">
<meta name="google-site-verification" content="MRuE10bVaHltuPvjYFH0PGfvJAuCZ2FScfAviXiKDFE" />
<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-4981483-8']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>
</head>
<body>
This another Google App Engine application!<br>
You can use it redirect the skydrive link.<br>
It's very simple:copy your skydrive link after the HOST,Then it's work.<br>
<br>
For Example:<br>
your skydrive link is <br>
http://cid-7978ee0a34b7f662.office.live.com/self.aspx/Public/EMS/2010/0627.mp3<br>
Then the static access link should be <br>
http://skydrive-gae.appspot.com/cid-7978ee0a34b7f662.office.live.com/self.aspx/Public/EMS/2010/0627.mp3<br>

remember remove the skydrive "http://"<br>

<br>
Thanks to <img src="http://msl.appspot.com/static/images/gmail.png"><br>
<br>
Modified by <img src="https://kuyur.info/blog/uploads/2010/03/mail.png"><br>
</body>
</html>''')
     
application = webapp.WSGIApplication(
                                     [('/', IndexPage),
                                     ('/index.html', IndexPage),
                                     ('/index.htm', IndexPage),
                                     ('/cid.*', MainPage),
                                     #(r'/http\*',MainPage),
                                     ],
                                     debug=True)

def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()

skydrive-gae下载(python2.5):
skydrive-gae

才发现SDUpload失效了,要Google的官方SDK才能上传

=====追记======
其实python2.5版用得好好的,但每次登录appengine,都吵着要你升级到python2.7和新版的DataStore
skydrive-gae并不需要读写DataStore,至于升级到python2.7,稍微修改一下代码即可。

# -*- coding: utf-8 -*-
import webapp2
import urllib2, httplib
import re

def getcid(link):
    pos = link.find('.office.live.com')
    return link[4:pos]

def to_new_type_link(link):
    pos = link.find('.office.live.com')
    cid = link[4:pos]
    uri = link[pos+len('.office.live.com'):]
    new_link = 'skydrive.live.com' + uri + '?cid=' + cid# + '&sc=documents'
    return new_link

def get_real_link(static_url):    
    curl = "http://" + static_url
    request = urllib2.Request(curl)
    opener = urllib2.build_opener()
    response = opener.open(request)
    realurl = response.geturl()
    return realurl

def get_dynamic_download_link(real_url, cid):
    pos = real_url.find(cid) + len(cid) + len('&id=')
    resid = real_url[pos:]
    return 'http://skydrive.live.com/download.aspx?cid=' + cid.upper() + '&resid=' + resid + '&canary='

def replace_html_code(old_link):
    new_link = old_link.replace('\\/','/')
    new_link = new_link.replace('https://','http://')
    return new_link

class MainPage(webapp2.RequestHandler):
    def get(self):
        old_type_link = self.request.path[1:]
        cid = getcid(old_type_link)
        new_type_link = to_new_type_link(old_type_link)
        real_link = get_real_link(new_type_link)
        self.response.headers['Content-Type'] = 'text/plain'
        self.redirect(replace_html_code(get_dynamic_download_link(real_link, cid)))
        
class IndexPage(webapp2.RequestHandler):
    def get(self):
        self.response.out.write('''<html>
<head>
<title>
skydrie-gae
</title>
<meta content="A Google App Engine application for redirect the skydrive link" name="description">
<meta content="skydrive, skydirve-gae, gae, google app engin," name="keywords">
<meta name="google-site-verification" content="MRuE10bVaHltuPvjYFH0PGfvJAuCZ2FScfAviXiKDFE" />
<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-4981483-8']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>
</head>
<body>
This another Google App Engine application!<br>
You can use it redirect the skydrive link.<br>
It's very simple:copy your skydrive link after the HOST,Then it's work.<br>
<br>
For Example:<br>
your skydrive link is <br>
http://cid-7978ee0a34b7f662.office.live.com/self.aspx/Public/EMS/2010/0627.mp3<br>
Then the static access link should be <br>
http://skydrive-gae.appspot.com/cid-7978ee0a34b7f662.office.live.com/self.aspx/Public/EMS/2010/0627.mp3<br>

remember remove the skydrive "http://"<br>

<br>
Thanks to <img src="http://msl.appspot.com/static/images/gmail.png"><br>
<br>
Modified by <img src="https://kuyur.info/blog/uploads/2010/03/mail.png"><br>
</body>
</html>''')
     
app = webapp2.WSGIApplication(
                                     [('/', IndexPage),
                                     ('/index.html', IndexPage),
                                     ('/index.htm', IndexPage),
                                     ('/cid.*', MainPage),
                                     #(r'/http\*',MainPage),
                                     ],
                                     debug=True)

app.yaml也要修改一下:

application: your-app-id
version: 9
runtime: python27
api_version: 1
threadsafe: true

handlers:
- url: /.*
  script: index.app

skydrive-gae下载(python2.7):
skydrive-gae

2013年1月14日 | 归档于 技术, 程序
本文目前尚无任何评论.

发表评论

XHTML: 您可以使用这些标签: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>
:wink: :-| :-x :twisted: :) 8-O :( :roll: :-P :oops: :-o :mrgreen: :lol: :idea: :-D :evil: :cry: 8) :arrow: :-? :?: :!: