首页 > 其他 > 详细

使用新浪图床

时间:2018-09-12 22:37:48      阅读:253      评论:0      收藏:0      [点我收藏+]

使用微博图床,简单上传图片返回外链

平时markdown用的频繁,当然需要一个简单好用的获得图片外链的方法。于是自己动手做了这个。
在我的网站上运行,先上图。
技术分享图片
首先申请个新浪微应用,授权获得auth2_token,后台实现上传图片后转发到微博,获得外链。
接收文件 view.py

@app.route("/mypic", methods=["GET", "POST"])
def mypic():
    t1 = [0,]
    if request.method == 'POST':
        t1[0] = 1
        f = request.files['file']
        pic_path='/s/jay/%s'%f.filename
        bkt.put_object(f.filename, f)
        t1.append(weibo.uploadpic(pic_path))
    else:
        t1.append('none')
    return render_template('uploadpic.html',t=t1)

weibo.uploadpic(pic_path)

def uploadpic(pic_path):
    s=session()
    wb_url = 'https://api.weibo.com/2/statuses/share.json'
    wb_content=pic_path+" 填充"
    d2 = {"access_token":"填充",  
    "status": wb_content }
    
    files={"pic":open(pic_path,"rb") }  
    rs = s.post(wb_url,data=d2,files=files)
    return str(rs.json()['original_pic'])

uploadpic.html

{%  extends 'base.html' %}
{% block page %}  
    <h2>微博图床,图片外链</h2>
        <div class="am-u-lg-12 am-u-md-12 am-u-sm-12">
          <form class="am-form" action="" method="POST" enctype="multipart/form-data">
            <label for="message" class="about-color">上传文件:</label>
            <br><input id="File1" type="file" name="file"/>
            <br>
            <button type="submit" class="am-btn am-btn-primary am-btn-sm">
            <i class="am-icon-check"></i> 上传</button>
          </form>
        </div>
<p>文件上传至微博账号 <a href="填充" target="_blank">填充</a></p>
<p>链接中已知可变参数:large thumbnail(缩略图) small </p>

{% if t[0] %}
    <p>图片上传成功! </p>
<script type="text/javascript">
function copyUrl2(){
var Url2=document.getElementById("biao1");
Url2.select(); // 选择对象
document.execCommand("Copy"); 
}
</script>
<textarea cols="60" rows="2" id="biao1">{{t[1]}}</textarea>
<input type="button" onClick="copyUrl2()" value="点击复制" />

{% endif %}
{% endblock %}

其他

  • 这个网页链接是私密的。站点访问量也极少,因此不用鉴权也很安全。
  • 关于其他 url 参数,试过新浪云存储的图片参数,并不支持。可能通过微博生成的图片外链只有这几(large/small)参数可选。
  • sae 屏蔽的python sdk 已知的有:sqlite3/qiniu

使用新浪图床

原文:https://www.cnblogs.com/aubucuo/p/sinapic.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!