forked from KichangKim/DeepDanbooru
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTagger.py
More file actions
87 lines (80 loc) · 2.15 KB
/
Tagger.py
File metadata and controls
87 lines (80 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import requests
import re
import os
# import json
from bs4 import BeautifulSoup
session = requests.Session()
def format_url(fileurl):
''' "a/a/b_c" => "b" '''
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 format_tags(html_doc):
soup = BeautifulSoup(html_doc,"html.parser")
print ("Getting Tags...")
p_nodes = soup.find_all('tr')
lists=[]
for p_node in p_nodes:
tag_now=p_node.get_text().strip()
if tag_now[0] != 'G':
if tag_now[0] != 'S':
if tag_now[6] != ':':
tmpstr=''
for chars in tag_now:
if chars != '\n':
tmpstr+=chars
else:
break
lists.append(tmpstr)
return lists
def tag_query(fileurl):
url = "http://kanotype.iptime.org:8003/deepdanbooru/upload"
fl = open(fileurl,'rb')
flee = format_url(fileurl)
flag = 0
for i in flee:
if flag == 1:
if i == 'p':
flee = 'png'
break
elif i == 'j':
flee = 'jpg'
break
if i == '.':
flag = 1
files = {'file': ('1.'+flee, fl, 'image/'+flee)}
r2 = session.post(url, data={"network_type": "general"}, files=files)
return format_tags(r2.text)
def add_tag(fileurl):
post_id = format_url(fileurl)
print("Tagging to ",post_id)
url = "https://example.com/api/post/" + post_id
headers = {
'Accept':'application/json',
'Authorization':'',
'Cookie':'',
'Content-Type':'application/json',
}
payload = {
"version":0,
"tags":tag_query(fileurl),
}
response = session.put(url, data=payload, headers=headers)
print(post_id,":",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):
print('Processing',file)
add_tag(path)