-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathsubscribestar_importer.py
138 lines (123 loc) · 5.41 KB
/
subscribestar_importer.py
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import sys
import psycopg2
import datetime
import config
import json
from gallery_dl import job
from gallery_dl import config as dlconfig
from gallery_dl.extractor.message import Message
from psycopg2.extras import RealDictCursor
from download import download_file, DownloaderException
from flag_check import check_for_flags
from proxy import get_proxy
from io import StringIO
from html.parser import HTMLParser
from os.path import join
from proxy import get_proxy
class MLStripper(HTMLParser):
def __init__(self):
super().__init__()
self.reset()
self.strict = False
self.convert_charrefs= True
self.text = StringIO()
def handle_data(self, d):
self.text.write(d)
def get_data(self):
return self.text.getvalue()
def strip_tags(html):
s = MLStripper()
s.feed(html)
return s.get_data()
def import_posts(key):
conn = psycopg2.connect(
host = config.database_host,
dbname = config.database_dbname,
user = config.database_user,
password = config.database_password,
cursor_factory = RealDictCursor
)
dlconfig.set(('output'), "mode", "null")
dlconfig.set(('extractor', 'subscribestar'), "cookies", {
"auth_token": key
})
dlconfig.set(('extractor', 'subscribestar'), "proxy", get_proxy())
j = job.DataJob("https://subscribestar.adult/feed")
j.run()
for message in j.data:
try:
if message[0] == Message.Directory:
post = message[-1]
file_directory = f"files/subscribestar/{post['author_name']}/{post['post_id']}"
attachments_directory = f"attachments/subscribestar/{post['author_name']}/{post['post_id']}"
cursor1 = conn.cursor()
cursor1.execute("SELECT * FROM dnp WHERE id = %s AND service = 'subscribestar'", (post['author_name'],))
bans = cursor1.fetchall()
if len(bans) > 0:
continue
check_for_flags(
'subscribestar',
post['author_name'],
str(post['post_id'])
)
cursor2 = conn.cursor()
cursor2.execute("SELECT * FROM booru_posts WHERE id = %s AND service = 'subscribestar'", (str(post['post_id']),))
existing_posts = cursor2.fetchall()
if len(existing_posts) > 0:
continue
stripped_content = strip_tags(post['content'])
post_model = {
'id': str(post['post_id']),
'"user"': post['author_name'],
'service': 'subscribestar',
'title': (stripped_content[:60] + '..') if len(stripped_content) > 60 else stripped_content,
'content': post['content'],
'embed': {},
'shared_file': False,
'added': datetime.datetime.now(),
'published': post['date'],
'edited': None,
'file': {},
'attachments': []
}
for attachment in list(filter(lambda msg: post['post_id'] == msg[-1]['post_id'] and msg[0] == Message.Url, j.data)):
if (len(post_model['file'].keys()) == 0):
filename, _ = download_file(
join(config.download_path, file_directory),
attachment[-1]['url'],
name = attachment[-1]['filename'] + '.' + attachment[-1]['extension']
)
post_model['file']['name'] = attachment[-1]['filename'] + '.' + attachment[-1]['extension']
post_model['file']['path'] = f'/{file_directory}/{filename}'
else:
filename, _ = download_file(
join(config.download_path, attachments_directory),
attachment[-1]['url'],
name = attachment[-1]['filename'] + '.' + attachment[-1]['extension']
)
post_model['attachments'].append({
'name': attachment[-1]['filename'] + '.' + attachment[-1]['extension'],
'path': f'/{attachments_directory}/{filename}'
})
post_model['embed'] = json.dumps(post_model['embed'])
post_model['file'] = json.dumps(post_model['file'])
for i in range(len(post_model['attachments'])):
post_model['attachments'][i] = json.dumps(post_model['attachments'][i])
columns = post_model.keys()
data = ['%s'] * len(post_model.values())
data[-1] = '%s::jsonb[]' # attachments
query = "INSERT INTO booru_posts ({fields}) VALUES ({values})".format(
fields = ','.join(columns),
values = ','.join(data)
)
cursor3 = conn.cursor()
cursor3.execute(query, list(post_model.values()))
conn.commit()
except DownloaderException:
continue
conn.close()
if __name__ == '__main__':
if len(sys.argv) > 1:
import_posts(sys.argv[1])
else:
print('Argument required - Login token')