这几天又想起来给图库加tag,就翻出了以前的tagger,结果发现无法使用,便部分重写了一次。
import requests,json,os,re
session = requests.Session() #这里还拼错了好几次
def get_version(post_id):
url = "https://img.sukasuka.cn/api/post/" + post_id
headers = {
'Accept':'application/json',
'Content-Type':'application/json'
}
resp = session.get(url,headers=headers)
try:
d = json.loads(resp.text)
except:
return 0
return int(d["version"])
def get_tag(fileurl):
url = 'http://dd.sukasuka.cn/upload'
files = {'fileField':open(fileurl,'rb')}#远端服务获取的label
resp = session.post(url,files=files)
files['fileField'].close()
d = resp.content.decode('utf8')
ans = []
try:
res = json.loads(d)
for i in res:
ans.append(i['tag'])
finally:
print(ans)
return ans
def format_url(fileurl):
tmpchr=""
tmpchrs=""
for chars in fileurl:
if chars == '/':
tmpchr = ""
else:
tmpchr+=chars
for chars in tmpchr:
if chars != '_':
tmpchrs+=chars
else:
break
return tmpchrs
def add_tag(fileurl):
post_id = format_url(fileurl)
if int(post_id) <= 4039 :
return
print("Tagging to ",post_id)
url = "https://img.sukasuka.cn/api/post/" + post_id
headers = {
'Accept': 'application/json',
'Accept-Language': 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
'Authorization': 'Token VGFnZXIFYmM0OTMCMDQtNDY0NC00Yz3dSTk5YTgtMDJlMTY1ZWZiNzJl',
'Content-Type': 'application/json',
'Cookie': '''auth={"user":"Tager","token":"b4493g04-4434-4d31-99a8-05e1f5efb72e"}''',
}
#以上的token仅供参考,已失效
payload = {"version":'1',"tags":[""],"source":"",}
payload['version'] = str(get_version(post_id))
payload['tags'] = get_tag(fileurl)
response = session.put(url, data=json.dumps(payload), headers=headers)
print("Done "+post_id+" "+str(response))
rootdir = '/var/local/szurubooru/data/posts/'
filelist = os.listdir(rootdir)
for file in filelist:
path = os.path.join(rootdir,file)
if os.path.isfile(path):
add_tag(path)
No responses yet