/
imgur.py
180 lines (137 loc) · 6.77 KB
/
imgur.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
from sqlalchemy.ext.automap import automap_base
Base = automap_base()
from sqlalchemy.ext.automap import automap_base
from sqlalchemy.orm import Session
from sqlalchemy import create_engine
from sqlalchemy import engine
Base = automap_base()
import time
import sys
from imgurpython import ImgurClient
import unirest
import os
client_id = '999ee789e084f2e'
client_secret = '0657e39557991653a45accdef5a16130491ff45a'
client = ImgurClient(client_id, client_secret)
reload(sys)
sys.setdefaultencoding('utf8')
start = time.time()
boards = ['sci']
engine = create_engine('mysql+pymysql://root:Supermon12'
'@localhost/chanarchive')
# This will run through the database and simple delete all images that are marked as dmca or illegal
def delete():
'''
Delete images
'''
# if img['dmca'] == 1 or img['illegal'] == 1:
# print board + '/' + img['threadID'] + "/" + img['local_thumbnail']
# query = '''UPDATE {0}_mod SET imgur_thumbnail_url = 'test' WHERE local_thumbnail = %s'''.format(board)
# engine.execute(query, img['local_thumbnail'])
# to_delete = ["692LfMR1HKotuic","nbxl2ZAcOTO2eoz", "nrsNcrYqUV0A9rH"]
to_delete = engine.execute("SELECT * FROM sci_mod")
# for row in to_delete:
# if row['dmca'] == 1 or row['illegal'] == 1:
# print "something"
for row in to_delete:
if (row['dmca'] == 1 or row['illegal'] == 1) and (row['imgur_thumbnail_url'] is not None or row['imgur_image_url'] is not None):
# client.delete_image(img)
# # These code snippets use an open-source library.
# These code snippets use an open-source library. http://unirest.io/python
response = unirest.delete("https://imgur-apiv3.p.mashape.com/3/image/{}".format(row['imgur_deletehash']),
headers={
"X-Mashape-Key": "y4mVnnNiZBmshBb9s7rh4DSw6K53p1T5SDujsnxxEUEvw62m4L",
"Authorization": "Client-ID 999ee789e084f2e",
"Accept": "text/plain"
}
)
print response.body['status']
if response.body['status'] == 200:
print "was successful"
# engine.execute("DELETE FROM imgur WHERE deletehash = {}".format(img[0]))
# query = '''DELETE FROM imgur WHERE deletehash = %s'''
# engine.execute(query, (img[0]))
def upload(image_name, board):
# This will upload the file
query_string = '''SELECT * FROM {0}_mod WHERE local_thumbnail = %s'''.format(board)
row = engine.execute(query_string, image_name)
for item in row:
# necessary variables
threadID = item['threadID']
thumbnail = item['local_thumbnail']
image = item['local_image']
directory_thumb = '/' + board + '/' + threadID + '/' + thumbnail
directory_image = '/' + board + '/' + threadID + '/' + image
"""
Upload the thumbnail
"""
try:
response = unirest.post("https://imgur-apiv3.p.mashape.com/3/image",
headers={
"X-Mashape-Key": "y4mVnnNiZBmshBb9s7rh4DSw6K53p1T5SDujsnxxEUEvw62m4L",
"Authorization": "Client-ID 999ee789e084f2e",
"Content-Type": "application/x-www-form-urlencoded",
"Accept": "application/json"
},
params={
"image": open(os.path.dirname(os.path.realpath(__file__)) + directory_thumb,
mode='r')
}
)
#
print response.body['data']['id']
print response.body['data']['link']
print response.body['data']['deletehash']
#
try:
image_thumbnail_id = response.body['data']['id']
imgur_thumbnail_url = response.body['data']['link']
deletehash = response.body['data']['deletehash']
query_string = '''UPDATE {0}_mod set imgur_thumbnail_id = %s, imgur_thumbnail_url = %s, imgur_thumbnail_deletehash = %s WHERE local_thumbnail = %s'''.format(board)
engine.execute(query_string, image_thumbnail_id, imgur_thumbnail_url, deletehash, image_name)
except Exception as e:
print e
pass
except:
pass
"""
Upload the main image
"""
try:
response = unirest.post("https://imgur-apiv3.p.mashape.com/3/image",
headers={
"X-Mashape-Key": "y4mVnnNiZBmshBb9s7rh4DSw6K53p1T5SDujsnxxEUEvw62m4L",
"Authorization": "Client-ID 999ee789e084f2e",
"Content-Type": "application/x-www-form-urlencoded",
"Accept": "application/json"
},
params={
"image": open(os.path.dirname(os.path.realpath(__file__)) + directory_image,
mode='r')
}
)
#
print response.body['data']['id']
print response.body['data']['link']
print response.body['data']['deletehash']
#
# try:
imgur_image_id = response.body['data']['id']
imgur_image_url = response.body['data']['link']
deletehash = response.body['data']['deletehash']
query_string = '''UPDATE {0}_mod set imgur_id = %s, imgur_image_url = %s, imgur_deletehash = %s WHERE local_thumbnail = %s'''.format(board)
engine.execute(query_string, imgur_image_id, imgur_image_url, deletehash, image_name)
except Exception as e:
print e
pass
for board in boards:
# Runs through and determines if a row either needs an upload of an image or a deletion
board_rows = engine.execute("SELECT * FROM {0}_mod".format(board))
for img in board_rows:
# print "another row"
if (img['moderated'] == 1 and img['approved'] == 1) and img['imgur_thumbnail_url'] is None and img['local_thumbnail'] is not None:
# print "Upload the images and update the database"
upload(img['local_thumbnail'], board)
# delete()
# Runs through entire database for any deletables
# delete()