Tumblr API - how to upload multiple images to a Photoset

◇◆丶佛笑我妖孽 提交于 2019-11-28 07:05:36

It's kind of a painful process, but I figured it out after studying the python code Tumblr posted. In short, it requires getting the hexadecimal for the photo and then doing some substitutions.

I've created a simple iOS / iPad / iPhone project on GitHub that uploads multiple photos to photosets using the Tumblr API, based heavily on the ASIHTTPRequest+OAuth project. It works so far in my limited testing -- feel free to make use of it and let me know how it goes.

The new PhotoUpLink for iPhone uploads true Tumblr photosets. It is a free download from the App Store at http://uplink.to/5o example photosets at http://photouplink.tumblr.com

The Tumblr uploader is based on @VictorVanHee's epic. I did optimize the code by switching to straight C for the NSData stringWithoutURLEncoding subroutine:

- (NSString *) stringWithoutURLEncoding
{
    NSString *hexDataDesc = [self description];
    hexDataDesc = [[hexDataDesc stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]] stringByReplacingOccurrencesOfString:@" " withString:@""];

    int hlen = [hexDataDesc length];

    NSString *hexDataDescU = [hexDataDesc uppercaseString];

    const char *hexcString = [hexDataDescU cStringUsingEncoding:NSASCIIStringEncoding];

    char *newStringC = malloc(hlen *3);
    memset(newStringC, 0, hlen *3); 

    int xC= 0, upd = 3000, value = 0;

    char *componentC = malloc(5);   // = "XX";

    componentC[2] = 0;

    const char *px = "%x"; char ptc = '%';

    for (int x=0; x<hlen; x+=2)
    {                           
        componentC[0] = hexcString[x];
        componentC[1] = hexcString[x+1];

        value = 0;
        sscanf(componentC, px, &value);
        if ((value <=46 && value >= 45) || (value <=57 && value >= 48) || (value <=90 && value >= 65) || (value == 95) || (value <=122 && value >= 97)) //48-57, 65-90, 97-122
        {  
            newStringC[xC++] = (char)value;
        }
        else
        {
            newStringC[xC++] = ptc;
            newStringC[xC++] = (char)componentC[0];
            newStringC[xC++] = (char)componentC[1];
        }
    }

    NSString *newString = [NSString stringWithCString:newStringC encoding:NSASCIIStringEncoding];
    NSString *aNewString = [newString stringByReplacingOccurrencesOfString:@"%20" withString:@"+"];

    free (newStringC);
    free (componentC);

    return aNewString;
}

I was having trouble with this too. Here is a gist in python of what ended up working for me: https://gist.github.com/charlesbrandt/11eadaec114288d621fa

The trick is that all photos must be added to your post parameters with 'data[#]' for keys. ('data[0]', 'data[1]', etc)

It's super simple, use 'source' parameter if you have only one image to upload, if there are multiple images, switch the parameter to 'data' and initialize it with the array of image URLs. For php tumblr client, following snippet can be used:

$paramsToPost = [
    'type' => 'photo',
    'tags' => 'tag1, tag2, tag2',
    'caption' => 'caption for photo(s)',
    'link' => 'http://example.com/click-through-url'
];
if (is_array($imageData)) {
    $paramsToPost['data'] = $imageData;
} else {
    $paramsToPost['source'] = $imageData;
}
$client = new Tumblr\API\Client($consumerKey, $consumerSecret);
$client->setToken($token, $tokenSecret);
$response = $client->createPost($blogName, $paramsToPost);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!