Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/php
- <?php
- // Fill these in...
- $user = "";
- $pass = "";
- $apiKey = "fd1e8237b859ceead8b6bc0549913e6fe254c609";
- $userid = -1;
- // The code below is for finding out your userid.
- $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);
- function error($msg, $exit=true) {
- shell_exec("zenity --error --text=".escapeshellarg($msg));
- if ($exit)
- exit(-1);
- }
- function confirm($msg) {
- return 1 - (int) shell_exec("zenity --question --ok-label=Yes --cancel-label=No --text=".escapeshellarg($msg).">/dev/null; echo $?");
- }
- function input($msg, $default="", $width=200) {
- if (is_array($default)) {
- $targ = "";
- foreach ($default as $t) {
- $t = trim($t);
- if (!empty($t))
- $targ = escapeshellarg($t) . " " . $targ;
- }
- $default = $targ;
- } else {
- }
- return trim(shell_exec("zenity --entry --text=\"$msg\" --entry-text=$default --width=$width"));
- }
- function exif2timestamp($exifString) {
- $exifPieces = explode(":", $exifString);
- return strtotime($exifPieces[0] . "-" . $exifPieces[1] . "-" . $exifPieces[2] . ":" . $exifPieces[3] . ":" . $exifPieces[4]);
- }
- 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))
- error("File does not exist: $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:
- error("Invalid EXIF rotation: $or");
- }
- }
- echo date("Y n j\n", exif2timestamp($exif['DateTime']));
- $edate = array('y' => (int) date("Y", exif2timestamp($exif['DateTime'])), 'm' => (int) date("n", exif2timestamp($exif['DateTime'])), 'd' => (int) date("j", exif2timestamp($exif['DateTime'])));
- var_dump($edate);
- $title = input("Enter title", file(getenv("HOME")."/.bdbuploadertitle"));
- file_put_contents(getenv("HOME")."/.bdbuploadertitle", "$title\n", FILE_APPEND);
- $text = input("Enter description", file(getenv("HOME")."/.bdbuploadertext"), 600);
- if (empty($text)) {
- error("Can not have empty description!");
- }
- file_put_contents(getenv("HOME")."/.bdbuploadertext", "$text\n", FILE_APPEND);
- // Extract date from directory
- $dir = basename(dirname(realpath($picture)));
- echo "$dir\n";
- if (!($ex = explode("-", $dir)))
- $ex = explode("_", $dir);
- $dateok = false;
- if ($ex && count($ex) == 3) {
- $ddate = array('y' => (int) $ex[0], 'm' => (int) $ex[1], 'd' => (int) $ex[2]);
- $dateok = confirm("Is the date {$ddate['y']}-{$ddate['m']}-{$ddate['d']} (found by directory) ok?");
- }
- if (!$dateok) {
- $year = (int) input("Enter year", date("Y"));
- $month = (int) input("Enter month", date("n"));
- $day = (int) input("Enter day", date("j"));
- $date = array('y' => $year, 'm' => $month, 'd' => $day);
- } else {
- $date = $ddate;
- }
- if (!checkdate($date['m'], $date['d'], $date['y'])) {
- error("Invalid date: {$date['y']}-{$date['m']}-{$date['d']}");
- }
- echo "Uploading file $picture, '$title', '$text', date: {$date['y']}-{$date['m']}-{$date['d']}\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/{$date['y']}/{$date['m']}/{$date['d']}.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);
- $response2 = curl_exec($ch);
- $response = json_decode($response2);
- if ($response == NULL) {
- var_dump($response2);
- var_dump($response);
- $tryagain = confirm("Failed (".curl_error($ch).") to upload file: $picture, Title: $title, Text: $text. Try again?");
- } else {
- $tryagain = false;
- shell_exec("notify-send \"File ".basename($picture)." successfully uploaded to date {$date['y']}-{$date['m']}-{$date['d']}!\"");
- }
- curl_close($ch);
- } while ($tryagain);
- var_dump($response);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement