Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- #######################################################################
- # #
- # Tumblr Reblog Bot v0.6 by saxgod 2011 #
- # #
- # Run in a 'screen' session with 'php ./reblogbot.php' and let it run #
- # to get a logfile use 'php ./reblogbot.php |tee log' #
- # #
- #######################################################################
- // Release by http://www.0mmo.net
- /*************************************************/
- /* parameters, configure according to your needs */
- /*************************************************/
- //login (email address) for the tumblr you are going to run this bot on.
- $tumblr_email = 'email@gmail.com';
- //password of the tumblr in question
- $tumblr_password = 'XXXXXXXX';
- //name of the tumble, eg if your url is demo.tumblr.com this would be 'demo'
- $tumblr_name = 'demo';
- //run every $sleep seconds
- $sleep = 300;
- //reblog what posttype ?
- //can be text, quote, photo, link, chat, video, or audio
- //comment out to allow all post types.
- $posttype = 'photo';
- //how many notes does a post need before reposting ?
- $defaultNeedNoteCount = 0;
- //you can add specific noteneeds per tumblr log name (for demo.tumblr.com this would be demo).
- $specificNeedNoteCount = array(
- //'demo' => 50,
- //'tumblrlog1' => 10,
- //'tumblrlog2' => 5,
- //'tumblrlog3' => 200
- /************************************************************************************************/
- /*etc.. never end the last one with a , (comma), all the others have to end with a , (comma) ! */
- /*if you have only one don't put in a , (comma) at the end. */
- /************************************************************************************************/
- );
- //how many notes can a post maximum have ? comment out to allow unlimited
- $maxnotecount = 2;
- //you can also add tumblrs to ignore. nice to ignore people you follow who are not in your niche
- //leave empty to disable.
- $ignorelist = array(
- //'ignorethisone',
- //'tubmlrnotinmyniche',
- //'idontlikethisone'
- );
- //if you set the whitelist, only tumblrs that are on here will be reblogged. all others will be ignored.
- //this might be handy if you want to follow a 'followtrain' or if this is a leaf in your tumblr-net
- //leave empty to disable.
- $whitelist = array(
- // '1',
- // '2',
- // '3',
- );
- //number of posts to reblog per time we get the dashboard xml.
- $reblogcount = 2;
- //set this to true if you only want to like posts (masslike) but not reblog them.
- //warning: the bot will not reblog them later on if you set this back to false since they are already liked.
- $likeonly = false;
- //where should we save the history ?
- $historyfile = 'history.txt';
- //urls of the api (as seen on http://www.tumblr.com/docs/en/api)
- //should not be changed unless the api url's change.
- $urls = array( 'dashboard' => 'http://www.tumblr.com/api/dashboard',
- 'reblog' => 'http://www.tumblr.com/api/reblog',
- 'like' => 'http://www.tumblr.com/api/like'
- );
- /*/*/ /*/*/ /*/*/ /*/*/ /*/*/ /*/*/ /*/*/ /*/*/ /*/*/ /*/*/ /*/*/ /*/*/ /*/*/ /*/*/ /*/*/ /*/*/ /*/*/ /*/*/
- /*********************************************************************************************************/
- /* do not edit below this line unless you know what you're doing !! */
- /*********************************************************************************************************/
- /*/*/ /*/*/ /*/*/ /*/*/ /*/*/ /*/*/ /*/*/ /*/*/ /*/*/ /*/*/ /*/*/ /*/*/ /*/*/ /*/*/ /*/*/ /*/*/ /*/*/ /*/*/
- /* main loop */
- $doubles=array();
- logger("Starting tumblr reblog-bot v0.6 for $tumblr_email");
- if (sizeof($whitelist) > 0) logger("Whitelist is enabled.");
- while (true) {
- $r = getDashboard();
- if ($r != null) {
- openHistory();
- $postinfo = getRandomPostsFromXML($r);
- if (sizeof($postinfo > 0)) {
- foreach ($postinfo as $post) {
- if (!$likeonly) doReblog($post);
- doLike($post);
- }
- }
- closeHistory();
- }
- logger("Sleeping $sleep seconds");
- sleep($sleep);
- }
- die();
- function openHistory() {
- global $doubles, $historyfile;
- $hfcontents = @file_get_contents($historyfile);
- $doubles = explode("\n", $hfcontents);
- }
- function closeHistory() {
- global $doubles, $historyfile;
- file_put_contents($historyfile, implode("\n", $doubles));
- $doubles = array();
- }
- function getDashboard() {
- global $tumblr_email, $tumblr_password, $posttype;
- logger("Getting dashboard via API");
- //data for dashboard
- $data = array();
- $data['email'] = $tumblr_email;
- $data['password'] = $tumblr_password;
- $data['likes'] = 1;
- if (isset($posttype))
- $data['type'] = $posttype;
- //get data from the dashboard
- $r = curlit('dashboard', $data);
- if ($r != null)
- logger("Received XML data");
- file_put_contents("in.xml", $r);
- return $r;
- }
- function doReblog($nPost) {
- global $tumblr_email, $tumblr_password, $doubles;
- $uniqueUrl = $nPost->getAttribute('reblogged-root-url');
- if (empty($uniqueUrl)) //if there is no reblogged-root-url this is a original post
- $uniqueUrl = $nPost->getAttribute('url-with-slug'); //this will later become the reblogged-root-url
- if (in_array($uniqueUrl, $doubles)) return; //check again!
- logger("Rebloging post {$nPost->getAttribute('id')} ({$nPost->getAttribute('reblog-key')}) from {$nPost->getAttribute('tumblelog')}");
- $data = array(
- 'email' => $tumblr_email,
- 'password' => $tumblr_password,
- 'post-id' => trim($nPost->getAttribute('id')),
- 'reblog-key'=> trim($nPost->getAttribute('reblog-key'))
- );
- $r = curlit('reblog', $data);
- if ($r != null) {
- //add post to the doubles list
- $doubles[] = $uniqueUrl;
- }
- }
- function doLike($nPost) {
- global $tumblr_email, $tumblr_password;
- logger("Liking post {$nPost->getAttribute('id')} ({$nPost->getAttribute('reblog-key')}) from {$nPost->getAttribute('tumblelog')}");
- if (trim($nPost->getAttribute('liked')) != 'true') {
- $data = array(
- 'email' => $tumblr_email,
- 'password' => $tumblr_password,
- 'post-id' => trim($nPost->getAttribute('id')),
- 'reblog-key'=> trim($nPost->getAttribute('reblog-key'))
- );
- $r = curlit('like', $data);
- if ($r != null)
- logger($r);
- } else {
- logger("Post is already liked (??)");
- }
- }
- function getRandomPostsFromXML($xmlstring) {
- global $specificNeedNoteCount, $defaultNeedNoteCount, $reblogcount, $tumblr_name, $ignorelist, $whitelist, $maxnotecount, $doubles;
- $xml = new DOMDocument();
- $xml->loadXML($xmlstring);
- $nlPosts = $xml->getElementsByTagName('post');
- $candidatePosts = array();
- foreach ($nlPosts as $nPost) {
- //get the postername
- $poster = $nPost->getAttribute('tumblelog');
- //are we using a whitelist ?
- if (sizeof($whitelist) > 0) {
- //only allow posters in the whitelist, ignore all others
- if (!in_array($poster, $whitelist))
- continue;
- }
- //check that the poster is not on the ignorelist
- if (in_array($poster, $ignorelist))
- continue;
- //don't reblog ourselves.... that looks stupid :p
- if ($tumblr_name == $poster)
- continue;
- //check if we already reblogged this in the past by checking the like flag
- if (trim($nPost->getAttribute('liked')) == 'true')
- continue;
- //check if we already reblogged this in the past by checking the doubles list
- $uniqueUrl = $nPost->getAttribute('reblogged-root-url');
- if (empty($uniqueUrl)) //if there is no reblogged-root-url this is a original post
- $uniqueUrl = $nPost->getAttribute('url-with-slug'); //this will later become the reblogged-root-url
- if (in_array($uniqueUrl, $doubles))
- continue;
- //check if we have specific notecounts for this poster
- if (array_key_exists($poster, $specificNeedNoteCount)) {
- $neednotecount = $specificNeedNoteCount[$poster];
- } else {
- $neednotecount = $defaultNeedNoteCount;
- }
- //check if we have enough notes for this post
- $notecount = (int) trim($nPost->getAttribute('note-count'));
- if ($notecount < $neednotecount)
- continue;
- if (isset($maxnotecount) && $notecount >= $maxnotecount)
- continue;
- //add it to the candidates for this run
- $candidatePosts[$nPost->getAttribute('id')] = $nPost;
- }
- $numCandidates = sizeof($candidatePosts);
- logger("Got $numCandidates candidate posts for reblogging");
- //do we have candidates ?
- if ($numCandidates == 0)
- return array();
- //do we have enough candidates to satisfy reblogcount ?
- if ($reblogcount > $numCandidates)
- $realReblogCount = $numCandidates;
- else
- $realReblogCount = $reblogcount;
- //get all ids
- $postIds = array_keys($candidatePosts);
- //select some at random
- $returnPosts = array();
- for($i=0; $i<$realReblogCount; $i++) {
- do {
- $selectedPost = rand(0,sizeof($postIds)-1);
- } while(key_exists($postIds[$selectedPost], $returnPosts));
- $returnPosts[$postIds[$selectedPost]] = $candidatePosts[$postIds[$selectedPost]];
- }
- //make sure the domobject is cleaned
- unset($xml);
- //return selected posts
- return $returnPosts;
- }
- function curlit($type, $postfields) {
- global $urls;
- $postfields = http_build_query($postfields);
- // Send the POST request (with cURL)
- $c = curl_init($urls[$type]);
- curl_setopt($c, CURLOPT_POST, true);
- curl_setopt($c, CURLOPT_POSTFIELDS, $postfields);
- curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
- //curl_setopt($c, CURLOPT_TIMEOUT, 240);
- $result = curl_exec($c);
- $status = curl_getinfo($c, CURLINFO_HTTP_CODE);
- $err = curl_error($c);
- if (!empty($err)) logger('cURL Error: ('.curl_errno($c).') '.$err);
- curl_close($c);
- // Check for success
- if ($status == 200) {
- return $result;
- } else if ($status == 201) {
- logger("Post successfull with ID $result");
- return $result;
- } else if ($status == 403) {
- logger("Bad email or password");
- return null;
- } else {
- logger("Error: $result");
- return null;
- }
- }
- function logger($log) {
- $time = date("H:i:s");
- echo "[$time] $log\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement