-
Notifications
You must be signed in to change notification settings - Fork 0
/
reddit2tumblr.py
241 lines (175 loc) · 10.2 KB
/
reddit2tumblr.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import os
import praw # reddit api
import urllib # view websites
import time # get time
from imgur_downloader import ImgurDownloader #get images (specifically gifv)
import pytumblr #tumblr api
import glob # for deleting things in the image folder
def read_secrets(filename):
try:
text = open(filename, 'r')
lines = []
for line in text:
line = line.replace('\n', '')
lines.append(line)
text.close()
return lines
except:
print('!!! Cannot find ' + filename + ' or it has been deleted !!!')
# SETTINGS
client = pytumblr.TumblrRestClient(read_secrets('secrets_tumblr')[0], # TUMBLR secrets. don't share
read_secrets('secrets_tumblr')[1],
read_secrets('secrets_tumblr')[2],
read_secrets('secrets_tumblr')[3])
blog_name = 'BLOGNAME' # your blog name on the url www.BLOGNAME.tumblr.com
r = praw.Reddit(client_id=read_secrets('secrets_reddit')[0], # REDDIT secrests. again don't share these
client_secret=read_secrets('secrets_reddit')[1],
user_agent='reddit2tumblr v.6 by rin')
# subreddit(s) you want to grab posts from. if you want to do more than one do "sub1+sub2"
subreddit = r.subreddit("SUBREDDITS")
min_score = 1 # min score a post can have to post
post_limit = 5 # number of posts you want to upload. if it fails to upload it still counts
post_sort = 'day' # how to tell reddit to sort them
delete_images_when_done = True # if you want the program to delete everything in the images folder when its done
def unic(msg): # convert text for saving in .txt
return msg.encode("utf-8")
def getTags(sub):
'''
You give it a subreddit and it will return a list of tags based on that subreddit
First it make the sub to be a string since praw passes an object instead of a string (???)
base_tags are the tags that will be applied to every single post.
Example: You run a meme blog and want every post to have the 'meme' tag in it, put it in here rather than typing it over and over
category_1_tags is another category with tags specific to that category. Obviously change the name to fit the category
Print statment is there to tell you what the subreddit was and to make sure the categories were right
Sets the tags to the base tags since every post gets them
Then the If statment part category part:
It checks if the input subreddit is the same as one of the subreddits listed in that category
Then prints out the output category name
Then adds the additional tags to the post tag list
I wouldn't use elif because some subs can be multiple categories
Example: r/wholesomebpt in my implementation of this program is both in the twitter category and the wholesome category
Make as many categories as you want. It doesn't have to be one
Just for some consistency, if the tags are the same as the base tags it will tell you
Once all the appropriate tags are added return the tags for the post
'''
sub = str(sub)
base_tags = ["example tag 1", "example tag 2"] # base tags are what every single post will get
category_1_tags = ["additional tag example 1", "aditional tag example 2"]
print("Input Sub: " + str.lower(sub))
tags = base_tags
# CATEGORY 1
if str.lower(sub) == ('example_subreddit') or str.lower(sub) == ('another_example_sub'):
print("Output Tags: CATEGORY NAME")
tags.extend(category_1_tags)
if tags == base_tags:
print("Output Tags: NORMIE")
return tags
def q_post(file_name, file_type, subreddit, caption): # i made q_post it's own function because i'm lazy and don't want to type out this post thing so much
if file_type == 'photo':
client.create_photo(blog_name, caption=caption, state='queue', tags=getTags(subreddit), data=file_name)
elif file_type == 'video':
client.create_video(blog_name, caption=caption, state='queue', tags=getTags(subreddit), data=file_name)
else:
print('Only photo and video supported currently')
def main():
# Initializing
current_dir = os.path.dirname(os.path.realpath(__file__)) # pytube download requires a full file path and using this to get it
print("opening file..") # open output in writing mode
target = open("output.txt", "w")
print("removing file..") # reset output
target.truncate()
print("writing file..")
print('getting submission')
submissions = subreddit.top(post_sort, limit=post_limit) # can be 'day' 'week' 'month' 'year' and probably 'all'e
print('obtianed submission')
with open('cache.txt', 'r') as cache: # go through all the cached posts
existing = cache.read().splitlines()
with open('cache.txt', 'a+') as cache: # with cache open
for submission in submissions: # go through all submissions gathered
time.sleep(.025) # wait so i can watch it work
if submission.id not in existing and submission.score >= min_score: # if we don't have it and it has enough points
# Adding to cache
existing.append(submission.id) # writes down the submission id so we post it again
cache.write(submission.id + '\n')
# Image sites
if submission.domain == 'i.imgur.com' or submission.domain == 'm.imgur.com' or submission.domain == 'imgur.com' or submission.domain == 'i.reddit.com':
print('\n______________________________\n')
# GIFs (but not GIFV)
if '.gif' in submission.url and '.gifv' not in submission.url:
print('File format: GIF')
urllib.request.urlretrieve(submission.url, 'images/' + submission.id + '.gif')
q_post('images/' + submission.id + '.gif', 'photo', submission.subreddit, submission.title)
continue
# JPG
elif '.jpg' in submission.url:
print('File format: JPG')
urllib.request.urlretrieve(submission.url, 'images/' + submission.id + '.jpg')
q_post('images/' + submission.id + '.jpg', 'photo', submission.subreddit, submission.title)
continue
# PNG
elif '.png' in submission.url:
print('File Format: PNG')
urllib.request.urlretrieve(submission.url, 'images/' + submission.id + '.png')
q_post('images/' + submission.id + '.png', 'photo', submission.subreddit, submission.title)
continue
# JPEG
elif '.JPEG' in submission.url:
print('File Format: JPEG')
urllib.request.urlretrieve(submission.url, 'images/' + submission.id + '.JPEG')
q_post('images/' + submission.id + '.JPEG', 'photo', submission.subreddit, submission.title)
continue
# YouTube
if submission.domain == 'youtu.be' or submission.domain == 'youtube.com':
print('\n______________________________\n')
print('File Format: Video (YouTube)')
try:
os.system('youtube-dl ' + submission.url + ' -o images/' + submission.id + '.mp4')
except:
print('!!! Could not download YouTube video !!!')
continue
# this if elif is if the video came from youtubehaiku
# they put stuff like this in front of the name, so i remove it
new_title = submission.title
if '[poetry]' in str.lower(submission.title):
new_title = submission.title[9:]
elif '[haiku]' in str.lower(submission.title):
new_title = submission.title[8:]
q_post(current_dir + '/images/' + submission.id + '.mp4', 'video', submission.subreddit, new_title)
continue
# GFYCAT
if '.gifv' in submission.url and submission.domain == 'gfycat.com':
print('\n______________________________\n')
print('File Format: GIF (GFYCAT)')
# gotta do substrings because the link reddit gives is wrong 100% of the time
gfycat_d = submission.url[:8] + 'thumbs.' + submission.url[8:] + '-size_restricted.gif'
try:
urllib.request.urlretrieve(gfycat_d, 'images/' + submission.id + '.gif')
except:
print("!!! Tried to download GFYCAT, failed !!!")
continue
q_post('images/' + submission.id + '.gif', 'photo', submission.subreddit, submission.title)
continue
# v.reddit.com
if submission.domain == 'v.reddit.com':
print('\n______________________________\n')
print('File format: Video (v.reddit)')
try:
os.system('youtube-dl ' + submission.url + ' -o images/' + submission.id + '.mp4')
except:
print('!!! Could not download YouTube video !!!')
continue
q_post('images/' + submission.id + '.mp4', 'video', submission.subreddit, submission.title)
continue
else:
print('Already have ' + submission.id + '!')
if delete_images_when_done:
folder = current_dir + '/images/*'
files = glob.glob(folder)
for f in files:
os.remove(f)
print('Images folder cleared')
print('!!! DONE STEALING MEMES !!!')
target.close() # unload the text
input()
if __name__ == "__main__":
main()