Advertisement
Guest User

Reblog Bot

a guest
Aug 7th, 2012
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.97 KB | None | 0 0
  1. <?php
  2.  
  3. #######################################################################
  4. #                                                                     #
  5. #               Tumblr Reblog Bot v0.6 by saxgod 2011                 #
  6. #                                                                     #
  7. # Run in a 'screen' session with 'php ./reblogbot.php' and let it run #
  8. # to get a logfile use 'php ./reblogbot.php |tee log'                 #
  9. #                                                                     #
  10. #######################################################################
  11. // Release by http://www.0mmo.net
  12. /*************************************************/
  13. /* parameters, configure according to your needs */
  14. /*************************************************/
  15.  
  16. //login (email address) for the tumblr you are going to run this bot on.
  17. $tumblr_email        =    'email@gmail.com';
  18.  
  19. //password of the tumblr in question
  20. $tumblr_password     =     'XXXXXXXX';
  21.  
  22. //name of the tumble, eg if your url is demo.tumblr.com this would be 'demo'
  23. $tumblr_name        =     'demo';
  24.  
  25. //run every $sleep seconds
  26. $sleep = 300;
  27.  
  28. //reblog what posttype ?
  29. //can be text, quote, photo, link, chat, video, or audio
  30. //comment out to allow all post types.
  31. $posttype = 'photo';
  32.  
  33. //how many notes does a post need before reposting ?
  34. $defaultNeedNoteCount = 0;
  35.  
  36. //you can add specific noteneeds per tumblr log name (for demo.tumblr.com this would be demo).
  37. $specificNeedNoteCount = array(
  38.     //'demo'       =>   50,
  39.     //'tumblrlog1' =>    10,
  40.     //'tumblrlog2' =>    5,
  41.     //'tumblrlog3' =>    200
  42.     /************************************************************************************************/
  43.     /*etc.. never end the last one with a , (comma), all the others have to end with a , (comma) !  */
  44.     /*if you have only one don't put in a , (comma) at the end.                                        */
  45.     /************************************************************************************************/
  46. );
  47.  
  48. //how many notes can a post maximum have ? comment out to allow unlimited
  49. $maxnotecount = 2;
  50.  
  51. //you can also add tumblrs to ignore. nice to ignore people you follow who are not in your niche
  52. //leave empty to disable.
  53. $ignorelist = array(
  54.     //'ignorethisone',
  55.     //'tubmlrnotinmyniche',
  56.     //'idontlikethisone'
  57. );
  58.  
  59. //if you set the whitelist, only tumblrs that are on here will be reblogged. all others will be ignored.
  60. //this might be handy if you want to follow a 'followtrain' or if this is a leaf in your tumblr-net
  61. //leave empty to disable.
  62. $whitelist = array(
  63. //    '1',
  64. //    '2',
  65. //    '3',
  66. );
  67.  
  68. //number of posts to reblog per time we get the dashboard xml.
  69. $reblogcount = 2;
  70.  
  71. //set this to true if you only want to like posts (masslike) but not reblog them.
  72. //warning: the bot will not reblog them later on if you set this back to false since they are already liked.
  73. $likeonly = false;
  74.  
  75. //where should we save the history ?
  76. $historyfile = 'history.txt';
  77.  
  78. //urls of the api (as seen on http://www.tumblr.com/docs/en/api)
  79. //should not be changed unless the api url's change.
  80. $urls = array(  'dashboard' =>  'http://www.tumblr.com/api/dashboard',
  81.                 'reblog'    =>  'http://www.tumblr.com/api/reblog',
  82.                 'like'      =>  'http://www.tumblr.com/api/like'
  83. );
  84.  
  85. /*/*/ /*/*/ /*/*/ /*/*/ /*/*/ /*/*/ /*/*/ /*/*/ /*/*/ /*/*/ /*/*/ /*/*/ /*/*/ /*/*/ /*/*/ /*/*/ /*/*/ /*/*/
  86. /*********************************************************************************************************/
  87. /*                   do not edit below this line unless you know what you're doing !!                    */
  88. /*********************************************************************************************************/
  89. /*/*/ /*/*/ /*/*/ /*/*/ /*/*/ /*/*/ /*/*/ /*/*/ /*/*/ /*/*/ /*/*/ /*/*/ /*/*/ /*/*/ /*/*/ /*/*/ /*/*/ /*/*/
  90.  
  91. /* main loop */
  92. $doubles=array();
  93. logger("Starting tumblr reblog-bot v0.6 for $tumblr_email");
  94. if (sizeof($whitelist) > 0) logger("Whitelist is enabled.");
  95. while (true) {
  96.     $r = getDashboard();
  97.     if ($r != null) {
  98.         openHistory();
  99.         $postinfo = getRandomPostsFromXML($r);
  100.         if (sizeof($postinfo > 0)) {
  101.             foreach ($postinfo as $post) {
  102.                 if (!$likeonly) doReblog($post);
  103.                 doLike($post);
  104.             }
  105.         }
  106.         closeHistory();
  107.     }
  108.     logger("Sleeping $sleep seconds");
  109.     sleep($sleep);
  110. }
  111. die();
  112.  
  113. function openHistory() {
  114.     global $doubles, $historyfile;
  115.    
  116.     $hfcontents = @file_get_contents($historyfile);
  117.     $doubles = explode("\n", $hfcontents);
  118. }
  119.  
  120. function closeHistory() {
  121.     global $doubles, $historyfile;
  122.    
  123.     file_put_contents($historyfile, implode("\n", $doubles));
  124.     $doubles = array();
  125. }
  126.  
  127. function getDashboard() {
  128.     global $tumblr_email, $tumblr_password, $posttype;
  129.    
  130.     logger("Getting dashboard via API");
  131.     //data for dashboard
  132.     $data = array();
  133.     $data['email'] = $tumblr_email;
  134.     $data['password'] = $tumblr_password;
  135.     $data['likes'] = 1;
  136.     if (isset($posttype))
  137.         $data['type'] = $posttype;
  138.     //get data from the dashboard
  139.     $r = curlit('dashboard', $data);
  140.     if ($r != null)
  141.         logger("Received XML data");
  142.     file_put_contents("in.xml", $r);
  143.     return $r; 
  144. }
  145.  
  146. function doReblog($nPost) {
  147.     global $tumblr_email, $tumblr_password, $doubles;
  148.  
  149.     $uniqueUrl = $nPost->getAttribute('reblogged-root-url');
  150.     if (empty($uniqueUrl)) //if there is no reblogged-root-url this is a original post
  151.         $uniqueUrl = $nPost->getAttribute('url-with-slug'); //this will later become the reblogged-root-url
  152.     if (in_array($uniqueUrl, $doubles)) return; //check again!
  153.    
  154.     logger("Rebloging post {$nPost->getAttribute('id')} ({$nPost->getAttribute('reblog-key')}) from {$nPost->getAttribute('tumblelog')}");
  155.     $data = array(
  156.         'email'     =>  $tumblr_email,
  157.         'password'  =>  $tumblr_password,
  158.         'post-id'   =>  trim($nPost->getAttribute('id')),
  159.         'reblog-key'=>  trim($nPost->getAttribute('reblog-key'))       
  160.     );
  161.     $r = curlit('reblog', $data);
  162.     if ($r != null) {
  163.         //add post to the doubles list
  164.         $doubles[] = $uniqueUrl;
  165.     }
  166. }
  167.  
  168. function doLike($nPost) {
  169.     global $tumblr_email, $tumblr_password;
  170.  
  171.     logger("Liking post {$nPost->getAttribute('id')} ({$nPost->getAttribute('reblog-key')}) from {$nPost->getAttribute('tumblelog')}");
  172.     if (trim($nPost->getAttribute('liked')) != 'true') {
  173.         $data = array(
  174.             'email'     =>  $tumblr_email,
  175.             'password'  =>  $tumblr_password,
  176.             'post-id'   =>  trim($nPost->getAttribute('id')),
  177.             'reblog-key'=>  trim($nPost->getAttribute('reblog-key'))       
  178.         );
  179.         $r = curlit('like', $data);
  180.         if ($r != null)
  181.             logger($r);
  182.     } else {
  183.         logger("Post is already liked (??)");
  184.     }
  185. }
  186.  
  187. function getRandomPostsFromXML($xmlstring) {
  188.     global $specificNeedNoteCount, $defaultNeedNoteCount, $reblogcount, $tumblr_name, $ignorelist, $whitelist, $maxnotecount, $doubles;
  189.    
  190.     $xml = new DOMDocument();
  191.     $xml->loadXML($xmlstring);
  192.     $nlPosts = $xml->getElementsByTagName('post');
  193.     $candidatePosts = array();
  194.     foreach ($nlPosts as $nPost) {
  195.         //get the postername
  196.         $poster = $nPost->getAttribute('tumblelog');
  197.         //are we using a whitelist ?
  198.         if (sizeof($whitelist) > 0) {
  199.             //only allow posters in the whitelist, ignore all others
  200.             if (!in_array($poster, $whitelist))
  201.                 continue;
  202.         }
  203.         //check that the poster is not on the ignorelist
  204.         if (in_array($poster, $ignorelist))
  205.             continue;
  206.         //don't reblog ourselves.... that looks stupid :p
  207.         if ($tumblr_name == $poster)
  208.             continue;
  209.         //check if we already reblogged this in the past by checking the like flag
  210.         if (trim($nPost->getAttribute('liked')) == 'true')
  211.             continue;
  212.         //check if we already reblogged this in the past by checking the doubles list
  213.         $uniqueUrl = $nPost->getAttribute('reblogged-root-url');
  214.         if (empty($uniqueUrl)) //if there is no reblogged-root-url this is a original post
  215.             $uniqueUrl = $nPost->getAttribute('url-with-slug'); //this will later become the reblogged-root-url
  216.         if (in_array($uniqueUrl, $doubles))
  217.             continue;
  218.        
  219.         //check if we have specific notecounts for this poster
  220.         if (array_key_exists($poster, $specificNeedNoteCount)) {
  221.             $neednotecount = $specificNeedNoteCount[$poster];
  222.         } else {
  223.             $neednotecount = $defaultNeedNoteCount;
  224.         }
  225.         //check if we have enough notes for this post
  226.         $notecount = (int) trim($nPost->getAttribute('note-count'));
  227.         if ($notecount < $neednotecount)
  228.             continue;
  229.         if (isset($maxnotecount) && $notecount >= $maxnotecount)
  230.             continue;
  231.            
  232.         //add it to the candidates for this run
  233.         $candidatePosts[$nPost->getAttribute('id')] = $nPost;
  234.     }
  235.     $numCandidates = sizeof($candidatePosts);
  236.     logger("Got $numCandidates candidate posts for reblogging");
  237.     //do we have candidates ?
  238.     if ($numCandidates == 0)
  239.         return array();
  240.    
  241.     //do we have enough candidates to satisfy reblogcount ?
  242.     if ($reblogcount > $numCandidates)
  243.         $realReblogCount = $numCandidates;
  244.     else
  245.         $realReblogCount = $reblogcount;
  246.    
  247.     //get all ids
  248.     $postIds = array_keys($candidatePosts);
  249.  
  250.     //select some at random
  251.     $returnPosts = array();
  252.     for($i=0; $i<$realReblogCount; $i++) {
  253.         do {
  254.             $selectedPost = rand(0,sizeof($postIds)-1);
  255.         } while(key_exists($postIds[$selectedPost], $returnPosts));
  256.         $returnPosts[$postIds[$selectedPost]] = $candidatePosts[$postIds[$selectedPost]];
  257.     }
  258.     //make sure the domobject is cleaned
  259.     unset($xml);
  260.     //return selected posts
  261.     return $returnPosts;
  262. }
  263.  
  264. function curlit($type, $postfields) {
  265.     global $urls;
  266.     $postfields = http_build_query($postfields);
  267.     // Send the POST request (with cURL)   
  268.     $c = curl_init($urls[$type]);
  269.     curl_setopt($c, CURLOPT_POST, true);
  270.     curl_setopt($c, CURLOPT_POSTFIELDS, $postfields);
  271.     curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
  272.     //curl_setopt($c, CURLOPT_TIMEOUT, 240);
  273.     $result = curl_exec($c);
  274.     $status = curl_getinfo($c, CURLINFO_HTTP_CODE);
  275.     $err = curl_error($c);
  276.     if (!empty($err)) logger('cURL Error: ('.curl_errno($c).') '.$err);
  277.     curl_close($c);
  278.    
  279.     // Check for success
  280.     if ($status == 200) {
  281.         return $result;
  282.     } else if ($status == 201) {
  283.         logger("Post successfull with ID $result");
  284.         return $result;
  285.     } else if ($status == 403) {
  286.         logger("Bad email or password");
  287.         return null;
  288.     } else {
  289.         logger("Error: $result");
  290.         return null;
  291.     }
  292. }
  293.  
  294. function logger($log) {
  295.     $time = date("H:i:s");
  296.     echo "[$time] $log\n";
  297. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement