Skip to content

Instantly share code, notes, and snippets.

@b1nary b1nary/_.md
Last active May 22, 2018

Embed
What would you like to do?
Tumblr Repost & Reddit post bots

Tumblr Repost & Reddit post bots

I've created these inspired by https://www.reddit.com/r/tumblrmoney/ before i realized its not worth it for me :) Maybe some of you can user it, the code is messy and could be optimized, its also not very beautiful and done in a hurry.

But the bot worked perfectly for the last month or so, so i figured why not post it when i dont use it anyway?

Installing & Using

It needs Ruby, this is super easy if you are not using Windows. If you use Windows install https://rubyinstaller.org/ The scripts need to be started on a command line. I guess you know what this is if on not Windows. On Windows start "cmd.exe" and navigate to the directory where the script is with:

dir C:\path\to\the\script

Once Ruby is installed you need to install the tumblr module too:

gem install tumblr_client
require 'tumblr_client'
require 'json'
EXCLUDE = ["dangerouslydifferentearthquake"]
PATH = File.dirname(__FILE__)
Tumblr.configure do |config|
config.consumer_key = "CONSUMER_KEY"
config.consumer_secret = "CONSUMER_SECRET"
config.oauth_token = "OAUTH_TOKEN"
config.oauth_token_secret = "OAUTH_SECRET"
end
client = Tumblr::Client.new
blogs = client.info["user"]["blogs"].map{|b| b["name"]}.reject{|b| EXCLUDE.include?(b)}
blogs.each do |b|
unless File.exists?(PATH+"/data/#{b}")
Dir.mkdir(PATH+"/data/#{b}")
Dir.mkdir(PATH+"/data/#{b}/cache")
File.open(PATH+"/data/#{b}/reblog.list", "w"){|f| f.write("")}
File.open(PATH+"/data/#{b}/tags.list", "w"){|f| f.write("")}
File.open(PATH+"/data/#{b}/blogged.ids", "w"){|f| f.write("[]")}
puts ":: created #{b}"
end
end
blogs.each do |b|
if File.exists?(PATH+"/data/#{b}/reblog.list")
begin
source = File.read(PATH+"/data/#{b}/reblog.list").split("\n").map{|l| l.gsub('http://','').gsub('/','')}.reject{|l|l.nil? || l.strip == ''}.sample
data = client.posts(source)["posts"].map {|post| {
title: post["title"], timestamp: post["timestamp"], id: post["id"],
reblog_key: post["reblog_key"], note_count: post["note_count"],
type: post["type"], tags: post["tags"] }
}.reject {|post| post[:type] != "photo"}
File.open(PATH+"/data/#{b}/cache/#{source}", "w"){|f| f.write(data.to_json)}
blogged = JSON.parse(File.read(PATH+"/data/#{b}/blogged.ids"))
search_post = true
while search_post
post_source = Dir.glob(PATH+"/data/#{b}/cache/*").reject{|r|r.include?('_reddit')}.sample
post_data = JSON.parse(File.read(post_source), {:symbolize_names => true})
while post_data.count > 0
post = post_data.shift
(search_post = false; break) if !blogged.include?(post["id"])
end
end
File.open(post_source, "w"){|f| f.write(post_data.to_json)}
blogged << post[:id]
File.open(PATH+"/data/#{b}/blogged.ids", "w"){|f| f.write(blogged.to_json)}
client.reblog(b, id: post[:id], reblog_key: post[:reblog_key])
rescue
end
end
end
require 'tumblr_client'
require 'json'
require 'open-uri'
PATH = File.dirname(__FILE__)
Tumblr.configure do |config|
config.consumer_key = "CONSUMER_KEY"
config.consumer_secret = "CONSUMER_SECRET"
config.oauth_token = "OAUTH_TOKEN"
config.oauth_token_secret = "OAUTH_SECRET"
end
client = Tumblr::Client.new
def fix_imgur url
url = url.gsub('//imgur','//i.imgur').split('.com/')
"#{url.first}.com/#{url.last}.jpg"
end
Dir.glob(PATH+"/data/**/reddit.list").each do |blog_path|
path = blog_path.gsub('/reddit.list','')
blog = path.split('/').last
reddit = File.read(blog_path).split("\n").reject{|l| l.nil? || l.strip == ''}.sample
reddit_raw = JSON.parse(open(reddit+"?limit=100", "User-Agent" => "Query is Love #1").read)["data"]["children"].map {|data|
{ id: data['data']['id'], url: fix_imgur(data['data']['url']), ups: data['data']['ups'] , sub: data['data']['subreddit'],
user: data['data']['author'], timestamp: data['data']['created'], title: data['data']['title']
} if data['data']['domain'][0..4] != 'self.' && !data['data']['url'].include?('reddituploads')
}.reject {|r| r.nil? || r[:ups].to_i < 3 }
File.open(PATH+"/data/#{blog}/cache/#{reddit.split('/').last.gsub('.json','_reddit')}", "w"){|f| f.write(reddit_raw.to_json)}
blogged = JSON.parse(File.read(PATH+"/data/#{blog}/blogged.ids"))
data_path = Dir.glob(PATH+"/data/#{blog}/cache/*_reddit").sample
data = JSON.parse(File.read(data_path), {:symbolize_names => true})
post = nil
while true
post = data.shift
break if !blogged.include?(post[:id])
end
File.open(data_path, "w"){|f| f.write(data.to_json)}
if !post.nil?
blogged << post[:id]
File.open(PATH+"/data/#{blog}/blogged.ids", "w"){|f| f.write(blogged.to_json)}
tags = File.read(PATH+"/data/#{blog}/tags.list").split("\n")
client.create_post(:photo, blog, tags: File.open(PATH+"/data/#{blog}/tags.list").read.chomp.split("\n").shuffle[0..5], format: 'markdown',
caption: "# #{post[:title]}\nby [/u/#{post[:user]}](https://reddit.com/u/#{post[:user]}) on by [/r/#{post[:sub]}](https://reddit.com/r/#{post[:sub]})",
source: post[:url])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.