<?php
/* This PHP script will download an image from gelbooru and upload it to a pleroma/mastodon account
Every time this script is called it will search gelbooru for the tags you specify, pick a random image and upload it to
your account
Updated: Should work with PHP 5.5 up to PHP 7
wrote by: @daisuke@stereophonic space
do whatever you want with this but don't come asking me for help if you mess up.
*/
$acc = "username:password";
$srv = "pl.instance.url";
//tags to search on gelbooru
$tags = "1girl solo";
//The text that will post the bot
$text = "#test";
//You shouldn't edit below here unless you know what all that shit means
$imageurl = gelbooru($tags);
if(!$imageurl){
die("No images found");
}
$imagefile = downloadpic($imageurl);
$image_id = uploadpic($imagefile);
$result = sendpost($text,$image_id);
echo "$result<br>";
unlink($imagefile);
function downloadpic($url){
$type = array_pop((array_slice(explode(".",$url), -1)));
$imgname = time().rand(1,200).".".$type;
$image = file_get_contents($url);
file_put_contents($imgname, $image);
return $imgname;
}
function gelbooru($query){
$sort = array("sort:score:desc","sort:score:asc","sort:id:desc","sort:id:asc","sort:width:desc","sort:width:asc");
$base = "http://gelbooru.com/index.php?page=dapi&s=post&q=index&tags=$query ".$sort[rand(0,5)];
$url = $base."&limit=1";
$xml=simplexml_load_file($url) or die("Couldn't connect to Gelbooru");
if ($xml['count'] == "0"){
return false;
} else {
$cnt = ($xml['count'] >= 20000 ? 19999 : $xml['count']-1);
list($usec, $sec) = explode(' ', microtime());
srand($sec + $usec * 2300000);
$post = rand(0,$cnt);
$url = $base."&pid={$post}&limit=1";
$xmlq=simplexml_load_file($url) or die("Gelbooru 2 - $url ");
}
if (empty($xmlq->post[0]['sample_url']) || is_null($xmlq->post[0]['sample_url'])){
return false;
} else {
return (is_numeric(strpos($xmlq->post[0]['sample_url'],"http")) ? "" : "http:").$xmlq->post[0]['sample_url'];
}
}
function uploadpic($file){
GLOBAL $acc,$srv;
$mime = mime_content_type($file);
$info = pathinfo($file);
$name = $info['basename'];
$output = new CURLFile($file, $mime, $name);
do{
$cSession = curl_init();
curl_setopt($cSession,CURLOPT_URL,"https://$srv/api/v1/media");
curl_setopt($cSession,CURLOPT_RETURNTRANSFER,true);
curl_setopt($cSession,CURLOPT_HEADER, false);
curl_setopt($cSession, CURLOPT_USERPWD, "$acc");
curl_setopt($cSession, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($cSession, CURLOPT_POST, 1);
@curl_setopt($cSession,CURLOPT_POSTFIELDS,array('file' => $output));
$result=curl_exec($cSession);
curl_close($cSession);
$file_id = json_decode($result,true)['id'];
}while(empty($file_id) || $file_id == "");
return $file_id;
}
function sendpost($text,$media_id){
GLOBAL $acc,$srv;
$cSession = curl_init();
curl_setopt($cSession,CURLOPT_URL,"https://$srv/api/v1/statuses");
curl_setopt($cSession,CURLOPT_RETURNTRANSFER,true);
curl_setopt($cSession,CURLOPT_HEADER, false);
curl_setopt($cSession, CURLOPT_USERPWD, "$acc");
curl_setopt($cSession, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($cSession, CURLOPT_POST, 1);
$query = "";
$query .= "status=".$text;
$query .= "&media_ids[]=".$media_id;
curl_setopt($cSession,CURLOPT_POSTFIELDS,$query);
$result=curl_exec($cSession);
curl_close($cSession);
return $result;
}
?>