Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/php
- <?php
- // Fill these in...
- $user = "";
- $pass = "";
- // You can get your own apikey here: http://bilddagboken.se/p/apiDoc.html
- // 40 characters of hexadecimal fun
- $apiKey = "";
- $userid = -1;
- // The commented code below is for finding out your userid.
- // Uncomment the code, read the userid from stdout and insert it into the script.
- // Or, uncomment and run the code for every picture you upload. Whatever...
- /* $rightTag = false;
- function startTag($prs, $data) {
- global $rightTag;
- if ($data == "USERID")
- $rightTag = true;
- }
- function contents($prs, $data) {
- global $rightTag, $userid;
- if ($rightTag) {
- $userid = (int) $data;
- $rightTag = false;
- echo "userid: $userid\n";
- }
- }
- $userinfoForm = "https://api.myphotodiary.com" . "/login_status.xml";
- $exec = "curl -H \"Expect:\" -u \"" . $user . ":" . $pass . "\" -F \"api_key=" . $apiKey . "\" " . $userinfoForm;
- $xml = shell_exec($exec);
- // echo "$xml\n";
- $prs = xml_parser_create();
- xml_set_element_handler($prs, "startTag", null);
- xml_set_character_data_handler($prs, "contents");
- xml_parse($prs, $xml);*/
- if (count($argv) > 1) {
- $picture = $argv[1];
- } else {
- $picture = trim(shell_exec("zenity --file-selection --file-filter=\"*.jpg\""));
- }
- $picture = realpath($picture);
- if (!file_exists($picture)) {
- if (!empty($picture))
- shell_exec("zenity --error --text=\"File does not exist: ".escapeshellarg($picture)."\"");
- exit(-1);
- }
- $exif = exif_read_data($picture);
- var_dump($exif);
- if ($exif && isset($exif['Orientation'])) {
- $or = $exif['Orientation'];
- switch ($or) {
- case 1: break;
- case 3: $rot = 2; break;
- case 6: $rot = 1; break;
- case 8: $rot = 3; break;
- default:
- shell_exec("zenity --error --text=\"Invalid EXIF rotation: $or\"");
- exit(-1);
- }
- }
- $title = file(getenv("HOME")."/.bdbuploadertitle");
- $title = $title[0];
- $title = trim(shell_exec("zenity --entry --text=\"Enter title\" --entry-text=\"".($title?$title:"")."\""));
- file_put_contents(getenv("HOME")."/.bdbuploadertitle", $title);
- $text = file(getenv("HOME")."/.bdbuploadertext");
- $text = $text[0];
- $text = trim(shell_exec("zenity --entry --text=\"Enter description\" --entry-text=\"".($text?$text:"")."\""));
- file_put_contents(getenv("HOME")."/.bdbuploadertext", $text);
- // Extract date from directory
- $dir = basename(dirname(realpath($picture)));
- echo "$dir\n";
- if (!($ex = explode("-", $dir)))
- $ex = explode("_", $dir);
- if ($ex) {
- $year = (int) $ex[0];
- $month = (int) $ex[1];
- $day = (int) $ex[2];
- }
- $datenotok = (int) shell_exec("zenity --question --ok-label=Yes --cancel-label=No --text=\"Is the date $year-$month-$day ok?\">/dev/null; echo $?");
- if ($datenotok) {
- $year = (int) trim(shell_exec("zenity --entry --text=\"Enter year\" --entry-text=\"`date +%Y`\""));
- $month = (int) trim(shell_exec("zenity --entry --text=\"Enter month\" --entry-text=\"`date +%m`\""));
- $day = (int) trim(shell_exec("zenity --entry --text=\"Enter day\" --entry-text=\"`date +%d`\""));
- }
- if (!checkdate($month, $day, $year)) {
- shell_exec("zenity --error --text=\"Invalid date: ".escapeshellarg("$year-$month-$day")."\"");
- exit(-1);
- }
- echo "Uploading file $picture, '$title', '$text', date: $year-$month-$day\n";
- do {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_HTTPHEADER, array("Expect:", "Api-Key: $apiKey"));
- curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
- curl_setopt($ch, CURLOPT_USERPWD, "$user:$pass");
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_URL, "https://api.myphotodiary.com/users/$userid/images/dates/$year/$month/$day.json");
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
- curl_setopt($ch, CURLOPT_POST, true);
- $parm = array("image" => "@$picture", "title" => "$title", "text" => "$text");
- if (isset($rot))
- $parm["rotation"] = $rot;
- curl_setopt($ch, CURLOPT_POSTFIELDS, $parm);
- $response = curl_exec($ch);
- $response = json_decode($response);
- if ($response == NULL) {
- $donttryagain = (int) shell_exec("zenity --question --ok-label=Yes --cancel-label=No --text=\"Failed to upload file: ".escapeshellarg($picture).", Title: ".escapeshellarg($title).", Text: ".escapeshellarg($text).". Try again?\">/dev/null; echo $?");
- } else {
- $donttryagain = true;
- shell_exec("notify-send \"File ".basename($picture)." successfully uploaded to date $year-$month-$day!\"");
- }
- } while (!$donttryagain);
- var_dump($response);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement