tumblr / tumblr.php

Tumblr API v2 PHP Client
Apache License 2.0
407 stars 115 forks source link

How to get video post URL #98

Open sexavet94 opened 6 years ago

sexavet94 commented 6 years ago

Hi, When i upload video post my response is:

object(stdClass)#1151 (3) { ["id"]=> float(174874832676) ["state"]=> string(11) "transcoding" ["display_text"]=> string(75) "Your video is still processing, please wait a few minutes for it to appear." }

When video transcoding is finished , new video post id changed and https://xxxxxx.tumblr.com/post/174874832676 - link returnd "Not found".

How i can get video post id after transcoding finished?

ravimisra commented 6 years ago

I fear that there is no direct way to find out the URL. Though, you can set up a poll for that as follows: Assuming $posting_init_on initialized to the DateTime of the time when we posted the video and $source_url to the source URL for the post.

$remote_url = null;
$attempts = 0;
$found = false;
do {
    $info = $client->getBlogPosts($blogName, ['type' => 'video', 'limit' => 1]);
    if (!empty($info) && !empty($info->posts) && !empty($info->posts[0])) {
        if ($info->posts[0]->timestamp >= $posting_init_on->timestamp) {
            $url = $info->posts[0]->source_url;
            $parsed_source_url = parse_url($url);
            $query = $parsed_source_url['query'];
            if (!empty($query)) {
                parse_str($query, $parsed_query);
                if (!empty($parsed_query) && $parsed_query['z'] == $source_url) {
                    $remote_url = $info->posts[0]->post_url;
                    $found = true;
                }
            }
        }
    }
    $attempts++;
    sleep(($attempts + 1) * 5);
} while ($attempts < 15 && $found === false);
oligriffiths commented 6 years ago

https://xxxxxx.tumblr.com/post/174874832676 Is the correct url, however video transcoding takes a few minutes to a few hours depending upon the size of the video and how many other videos are being processed at the same time. Your best bet is to periodically check if the video url returns a non 404.

ravimisra commented 6 years ago

@oligriffiths, the id returned in the transcoding response is just to reference transcoding process, it never matches with the final post id. So https://xxxxxx.tumblr.com/post/174874832676 is wrong URL for the final post.