之前在用 hexo搭建博客的時(shí)候一直想弄個(gè)圖床,在網(wǎng)上搜了許多方法都覺得不好。
后來發(fā)現(xiàn)簡(jiǎn)書的寫文章頁面可以上傳圖片,于是萌生了利用簡(jiǎn)書的圖片上傳功能來搭建一個(gè)圖床的想法。
下面是具體實(shí)現(xiàn),詳細(xì)的可以看代碼實(shí)現(xiàn),整體不是很難只要拿到 cookie然后上傳圖片即可。
關(guān)鍵代碼:
cookie:簡(jiǎn)書登錄之后的 cookie
filepath:要上傳圖片的絕對(duì)路徑,同目錄下可直接使用名字
filename:要上傳圖片的名字(隨意?。?/p>
def uploadImage(cookie, filepath, filename):
upload_url = 'https://upload.qiniup.com/'
token_url = 'http://www.itdecent.cn/upload_images/token.json?filename={}'.format(filename)
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.117 Safari/537.36',
'Cookie': cookie,
}
response = requests.get(token_url, headers=headers)
response.encoding = response.apparent_encoding
token_and_key = json.loads(response.text)
with open(filepath, 'rb') as file:
files = {
'file': (filename, file),
'token': (None, token_and_key['token']),
'key': (None, token_and_key['key']),
}
response = requests.post(upload_url, headers=headers, files=files)
response.encoding = response.apparent_encoding
return json.loads(response.text)['url']
PyQt4封裝后的代碼:
與腳本同目錄下創(chuàng)建一個(gè)名為 config的文件(沒有后綴名),用文本編輯器打開(別用記事本,如果用記事本打開并保存過請(qǐng)刪除重建),將簡(jiǎn)書登錄后的 cookie直接粘貼進(jìn)去(不需要多余的字符,只要 cookie就行)
代碼附上:
#-*- coding: utf-8 -*
__author__ = 'geebos'
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import requests
import json
import sys
class UploadBox(QDialog):
def __init__(self):
QDialog.__init__(self)
layout = QVBoxLayout()
self.data = {}
self.cookie = self.getCookie().strip()
self.location_lable = QLabel("圖片位置:")
self.url_lable = QLabel("圖片鏈接:")
self.show_path = QLineEdit()
self.show_result = QTextBrowser()
self.select_button = QPushButton("選擇圖片")
self.comfir_button = QPushButton("確認(rèn)上傳")
layout.addWidget(self.location_lable)
layout.addWidget(self.show_path)
layout.addWidget(self.url_lable)
layout.addWidget(self.show_result)
layout.addWidget(self.select_button)
layout.addWidget(self.comfir_button)
self.setWindowTitle("圖片上傳")
self.setLayout(layout)
self.select_button.clicked.connect(self.selectFile)
self.comfir_button.clicked.connect(self.uploadImage)
def getCookie(self):
try:
with open('config', 'r') as f:
return f.readline()
except Exception:
QMessageBox.warning(self, "提示", "配置文件 config出錯(cuò)")
def selectFile(self):
filepath = QFileDialog.getOpenFileName(self, caption="選擇圖片", directory='.', filter="ALL FILES (*.*)")
self.show_path.setText(filepath)
self.data['filepath'] = filepath
def uploadImage(self):
if 'filepath' in self.data:
filepath = self.data['filepath']
filename = filepath.split('/')[-1]
print(filename)
else:
return
upload_url = 'https://upload.qiniup.com/'
token_url = 'http://www.itdecent.cn/upload_images/token.json?filename={}'.format(filename)
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.117 Safari/537.36',
'Cookie': self.cookie,
}
response = requests.get(token_url, headers=headers)
response.encoding = response.apparent_encoding
token_and_key = json.loads(response.text)
if 'token' not in token_and_key:
self.show_result.clear()
QMessageBox.warning(self, "提示", "格式錯(cuò)誤,請(qǐng)選擇圖片")
return
with open(filepath, 'rb') as file:
files = {
'file': (filename, file), 'token': (None, token_and_key['token']), 'key': (None, token_and_key['key']),
}
response = requests.post(upload_url, headers=headers, files=files)
response.encoding = response.apparent_encoding
result = json.loads(response.text)
if 'url' in result:
self.show_result.clear()
self.show_result.append(result['url'])
else:
self.show_result.clear()
QMessageBox.Information(self, "提示", "上傳失敗,請(qǐng)檢查 cookie是否有效")
app = QApplication(sys.argv)
dialog = UploadBox()
dialog.show()
app.exec_()