Flickr API / Discuss

Current Discussion

Searching users on the API
Latest: 12 hours ago
2048 and 1600 size
Latest: 3 days ago
New Group APIs
Latest: 3 days ago
Null url field in oembed response
Latest: 4 days ago
flexplore
Latest: 4 days ago
Display a specific album in WebSite
Latest: 4 days ago
oEmbed doesn't provide large size URL?
Latest: 5 days ago
ObjectiveFlickr Framework 2.0 for Mac and iPhone Released
Latest: 6 days ago
Survey on Web APIs Popularity and Support Forums
Latest: 6 days ago
Flickr API changed within the last 24 hours? its broken suddenly
Latest: 6 days ago
New API to modify camera information of the photo?
Latest: 8 days ago
Who is admin and where is my item in the pool
Latest: 8 days ago
More...

Search this group's discussions

manufacturing flic.kr style photo URLs

view profile

kellan is a group administrator kellan  Pro User  says:

Super quick note, hopefully sufficient info.

The format for the short photo URLs is

flic.kr/p/{short-photo-id}

A short photo id is a base58 conversion of the photo id. Base58 is like base62 [0-9a-zA-Z] with some characters removed to make it less confusing when printed. (namely 0, O, I, and l).

So that leaves an alphabet of: 123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ

I'm including below a variation of the code we use to do base conversion. Note it doesn't use modulus because PHP's modulus operator overflows for large numbers (like photo ids)


function base_encode($num, $alphabet) {
$base_count = strlen($alphabet);
$encoded = '';
while ($num >= $base_count) {
$div = $num/$base_count;
$mod = ($num-($base_count*intval($div)));
$encoded = $alphabet[$mod] . $encoded;
$num = intval($div);
}

if ($num) $encoded = $alphabet[$num] . $encoded;

return $encoded;
}

function base_decode($num, $alphabet) {
$decoded = 0;
$multi = 1;
while (strlen($num) > 0) {
$digit = $num[strlen($num)-1];
$decoded += $multi * strpos($alphabet, $digit);
$multi = $multi * strlen($alphabet);
$num = substr($num, 0, -1);
}

return $decoded;
}

Originally posted at 9:42AM, 13 April 2009 PDT (permalink)
kellan edited this topic 38 months ago.

view photostream

blech​  Pro User  says:

Thank you. I was trying to reverse engineer the base58 string, and getting close, but no cigar. (If the photo ID space wasn't quite so sparsely populated where I was sampling it, I might have managed, too.)
Posted 38 months ago. (permalink)

view photostream

alto maltés says:

Cool.
Posted 38 months ago. (permalink)

view photostream

matt  Pro User  says:

Someone jam this in a twitter client, pronto!
Posted 38 months ago. (permalink)

view photostream

Eli the Bearded  Pro User  says:

BC math is available in PHP.

bcmod -- Get modulus of an arbitrary precision number
Posted 38 months ago. (permalink)

view photostream

Richard Cunningham  Pro User  says:

It would be good if this was translatable to a photo url with out any requests (in the way twitpic, twitgoo, yfrog etc. do).

Just knowing the photo id doesn't get you to photo url since you need the secret, farm and server (http://www.flickr.com/services/api/misc.urls.html). We would have to do a flickr.photos.getInfo for each photo.

unless I'm missing something?
Posted 38 months ago. (permalink)

view photostream

fraserspeirs  Pro User  says:

Here's an implementation of the encoding part in Objective-C: gist.github.com/101674
Posted 38 months ago. (permalink)

view photostream

Jiayong Ou says:

And here's an implementation in Ruby:

gist.github.com/101753
Posted 38 months ago. (permalink)

view photostream

faygate  Pro User  says:

C# example:

http://www.faygate.net/post/133462295/tinyurlcode
Posted 35 months ago. (permalink)

view photostream

Xenocryst @ Antares Scorpii says:

An example for a bookmarklet:

javascript:void(prompt('Flic.kr short ID (base58)',(function(num){if(typeof num!=='number')num=parseInt(num);var enc='', alpha='123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ'; var div=num,mod;while(num>=58){div=num/58;mod=num-(58*Math.floor(div));enc=''+alpha.substr(mod,1) +enc;num=Math.floor(div);} return(div)?''+alpha.substr(div,1)+enc:enc;})(prompt('Flickr picture ID'))))


ETA: A more useful variant, giving you the respective Flic.kr-URL, if activated on a photo display page, might be:

javascript:void((function(){var m=window.location.href.match(/^https?:\/\/[^/]*\bflickr\.com\/(photos\/[^/]+\/(\d+))/i);if(m.length&&m[2]) prompt('Short Flic.kr-URL for\n"'+m[1]+'":','http://flic.kr/p/'+(function(num)
{if(typeof num!=='number')num=parseInt(num);var enc='';var alpha='123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ';var div=num; while(num>=58){div=num/58;var mod=(num-(58*Math.floor(div)));enc=''+alpha.substr(mod,1)+enc;n um=Math.floor(div);}
return(div)?''+alpha.substr(div,1)+enc:enc;})(m[2]));})())

Originally posted 35 months ago. (permalink)
dopiaza (a group admin) edited this topic 22 months ago.

view photostream

stevefaeembra  Pro User  says:

quick python snippet for encoding...

num=12345678
a='123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ'
bc=len(a)
enc=''
while num>=bc:
    div,mod=divmod(num,bc)
    enc = a[mod]+enc
    num = int(div)
enc = a[num]+enc
print "flic.kr/p/%s" % (enc,)
Originally posted 35 months ago. (permalink)
stevefaeembra edited this topic 35 months ago.

view photostream

taiyofj  Pro User  says:

javascript decoder.

function base58_decode( snipcode )
{
    var alphabet = '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ' ;
    var num = snipcode.length ;
    var decoded = 0 ;
    var multi = 1 ;
    for ( var i = (num-1) ; i >= 0 ; i-- )
    {
        decoded = decoded + multi * alphabet.indexOf( snipcode[i] ) ;
        multi = multi * alphabet.length ;
    }
    return decoded;
}

I'm using above base 58 decode function on pbtweet
Originally posted 35 months ago. (permalink)
taiyofj edited this topic 35 months ago.

view photostream

Wonderm00n  Pro User  says:

Great topic kellan!

I've just used your base_encode function on flicktotwitt.com

THANKS!
Posted 35 months ago. (permalink)

view photostream

Tim Parenti  Pro User  says:

Thanks for the information. I've created a simple, web-based implementation at http://www.timparenti.com/dev/flickr/shortlink/

It's meant for the people who don't necessarily want or need Twitter integration, but just want to convert URLs (although I might add Twitter integration later).

It can handle conversions from a short URL to a photo ID and vice-versa. I'm planning to add full URL support so you can just paste a long URL from your browser bar and it will extract the relevant bit (the photo ID).

If anyone has any ideas to improve it, please let me know. Here's hoping someone at least finds it useful.
Originally posted 35 months ago. (permalink)
Tim Parenti edited this topic 35 months ago.

view photostream

burntoutcar  Pro User  says:

Any reason why this can't be incorporated into the "Share This" widget at the top right of each photo page?
Posted 35 months ago. (permalink)

view photostream

kellan is a group administrator kellan  Pro User  says:

Btw valid paths after the photo id will be passed along now.

So flic.kr/p/$photoid/sizes/l works as do flic.kr/p/$photoid/nearby and flic.kr/p/$photoid/favorites
Posted 35 months ago. (permalink)

view photostream

Kohichi  Pro User  says:

python decoder snippet
This code based on taiyofj's javascript one,


def b58decode(s):
    alphabet = '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ'
    num = len(s)
    decoded = 0 ;
    multi = 1;
    for i in reversed(range(0, num)):
        decoded = decoded + multi * ( alphabet.index( s[i] ) )
        multi = multi * len(alphabet)
    return decoded;

Posted 35 months ago. (permalink)

view photostream

Rex Roof  Pro User  says:

If these are really Base58, why doesn't the Math:Int2Base perl module work?
Posted 34 months ago. (permalink)

view photostream

Rex Roof  Pro User  says:

Oooh, because you aren't using regular Base58. you're using Base62 with your own characters removed. Sorry, answered my own question.
Posted 34 months ago. (permalink)

view photostream

robceemoz  Pro User  says:

my colleague Johnath converted this to a shorter Firefox bookmarklet using the page's contained <link rev="canonical"> tag:

javascript:var%20l=document.querySelectorAll("link");for(i=l.length-1;i>=0;--i){if(/flic\.kr/.test(l.item(i).href))prompt("Have%20a%20url!",l.item(i).href);}
Originally posted 34 months ago. (permalink)
robceemoz edited this topic 34 months ago.

view photostream

cmort  Pro User  says:

and a simple variation of @robceemoz's post to send it straight to twitter

javascript:var%20l=document.querySelectorAll("link"); for(i=l.length-1;i>=0;--i){if(/flic\.kr/.test(l.item(i).href)) document.location='http://twitter.com/?status=Just%20posted:%20'+l.item(i).href;}
Originally posted 34 months ago. (permalink)
dopiaza (a group admin) edited this topic 18 months ago.

view photostream

bitrot  Pro User  says:

I've just posted up a Greasemonkey script based on Xenocryst @ Antares Scorpii's bookmarklet (above):

flic.kr Short URL Link
at userscripts.org

It looks like this:

flic.kr short URL screenshot

Hope you find it useful!
Originally posted 34 months ago. (permalink)
bitrot edited this topic 34 months ago.

view photostream

kellan is a group administrator kellan  Pro User  says:

Rex, don't know if you're still looking for a Perl module, but check out
search.cpan.org/~miyagawa/Encode-Base58-0.01/lib/Encode/B...
Posted 34 months ago. (permalink)

view photostream

andrhamm says:

Will there ever be the ability to use this URL shortening for sets?
Originally posted 31 months ago. (permalink)
andrhamm edited this topic 31 months ago.

view photostream

opello  Pro User  says:

:
I revised your initial check against m as such:


if(m&&m.length&&m[2])

So that it doesn't introduce a javascript error when used on non-flickr pages (accidental clicks). I didn't see adding further error checking as worthwhile, but someone else might.
Posted 31 months ago. (permalink)

view photostream

Zoolcar9 says:

I wrote a Firefox extension to get flic.kr shortened URL from context menu if you right click on Flickr photo, link, or Flickr photo page. Please try it
zoolcar9.lhukie.net/extensions/flic.kr.xpi

Please report any issues or suggestions at
www.flickr.com/photos/zoolcar9/sets/72157622750210617/com...
or at Flickr Hacks group
www.flickr.com/groups/flickrhacks/discuss/72157622750913301/
 
Originally posted 30 months ago. (permalink)
Zoolcar9 edited this topic 30 months ago.

view photostream

frog23-net says:

Here is the Java Version:
dl.dropbox.com/u/1844215/FlickrBaseEncoder.java
Posted 30 months ago. (permalink)

view photostream

Richard Barnett  Pro User  says:

Any chance of m.flic.kr redirecting to an m.flickr.com page?
Posted 30 months ago. (permalink)

view photostream

flowolf says:

i wrote a bash version to convert a photo ID into the base58 photo id and vice versa:
klienux.org/scripts/base58.sh.txt

have fun
Posted 29 months ago. (permalink)

view photostream

kellan is a group administrator kellan  Pro User  says:

And I've added the much requested img urls.

flic.kr/p/7sqMtA
flic.kr/p/img/7sqMtA.jpg
flic.kr/p/img/7sqMtA_s.jpg
flic.kr/p/img/7sqMtA_t.jpg
flic.kr/p/img/7sqMtA_m.jpg

Generalized form:

flic.kr/p/img/$shortid_[stm].jpg

Fun fact: this was built for del.icio.us many years ago, I just added support for the short url identifiers.

Happy New Year
Posted 29 months ago. (permalink)

view photostream

Jan Hapke says:

It would be cool if this also worked for sets. So we could use URLs like

flic.kr/s/aHsj9AnSbu

Or generally more functions of flickr, like maybe slide shows, collections, you name it.

The problem is that set ids are very long (for some reason longer than photo ids) so the resulting short URLs might still be too long for twitter.
Posted 28 months ago. (permalink)

view photostream

PetroleumJelliffe  Pro User  says:

Does the URL shortener only work for photos because you can still access photos with just their ID using this:

www.flickr.com/photo.gne?id=2293005459

There isn't an equivalent for sets that I know of. That is, you need to know who created a set to access one, but for photos you don't.
Posted 28 months ago. (permalink)

view photostream

Sybren A. Stüvel  Pro User  says:

Support for the short photo page URL has been included in version 1.4 of the Python FlickrAPI kit, released today. For more information see stuvel.eu/archive/119/python-flickr-api-14-released
Posted 28 months ago. (permalink)

view photostream

jolinwarren says:

Here's an AppleScript version of the encoder in this post:


on base_encode(alphabet, num)
set base_count to count of alphabet
set encoded to ""
repeat while (num ≥ base_count)
set encoded to (item ((num mod base_count as integer) + 1) of alphabet) & encoded
set num to num div base_count
end repeat

if (num > 0) then set encoded to (item (num + 1) of alphabet) & encoded

return encoded
end base_encode

Posted 26 months ago. (permalink)

view photostream

tjieftjaf  Pro User  says:

I use this function in MySQL:


DELIMITER $$

DROP FUNCTION IF EXISTS `BASE58` $$
CREATE FUNCTION `BASE58`( num BIGINT) RETURNS varchar(255)
DETERMINISTIC
BEGIN
DECLARE chars CHAR(58) DEFAULT "123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
DECLARE stream CHAR(32) DEFAULT "";
DECLARE res INT;
WHILE num > 0 DO
SET res = num % 58;
SET num = num DIV 58;
SET stream = CONCAT(MID( chars, res + 1, 1 ), stream);
END WHILE;
RETURN stream;

END $$

DELIMITER ;


And then in your SELECT, INSERT, etc.:

SELECT Title, CONCAT('http://flic.kr/p/', BASE58(Id)) FROM `Photo`;


Based on a post by Barry Gould on the MySQL forum.
Posted 23 months ago. (permalink)

view photostream

whatsthatpicture  Pro User  says:

Hi, I'm not after Flickr's trade secrets, but is there any broad evidence as to how successful the implementation of short urls has been on Flickr, and how much they are used? I'm trying to get a feel for whether this is worth doing on a museums collection catalogue.
Originally posted 22 months ago. (permalink)
whatsthatpicture edited this topic 22 months ago.

view photostream

dopiaza is a group administrator dopiaza  Pro User  says:

I think it depends on how you expect your catalogue to get used. I see the short URLs bandied around a lot on Twitter, where space is at a premium, but I don't really see them much in other contexts.

When sharing URLs, I tend to prefer the long versions - you get more immediate contextual information that way.
Posted 22 months ago. (permalink)

view photostream

scoopedup20 says:

where is the callback url field
Posted 18 months ago. (permalink)

view photostream

andre.laszlo says:

It should be noted that if you try kellan's php algorithm above and get your id from the rest api interface for example, you get utf-8 encoded data and the algorithm will not work unless you convert it to something that php can handle. Using iconv() for example. This is what happens when you end up with 4gLq58 all the time :)
Posted 16 months ago. (permalink)

view photostream

alavoy says:

I posted an update to the Base 58 Objective-C Encoder to now include Decoding:

gist.github.com/819739
Posted 16 months ago. (permalink)

view photostream

Milgrim says:

more python code:


alphabet = '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ'
base = len(alphabet)

def b58encode(div, s=''):
    if div >= base:
        div, mod = divmod(div, base)
        return b58encode(div, alphabet[mod] + s)
    return alphabet[div] + s

def b58decode(s):
    return sum(alphabet.index(c) * pow(base, i) for i, c in enumerate(reversed(s)))

Posted 15 months ago. (permalink)

view photostream

tjieftjaf  Pro User  says:

It looks like flic.kr/s/{short-set-id} is now working too!
Posted 12 months ago. (permalink)

view photostream

daruyanagi says:

by C# (string class extention)

const string BASE58 = "123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";

public static long Base58Decode(this string input)
{
return BaseDecode(input, BASE58);
}

public static long BaseDecode(string input, string alphabet)
{
long decoded = 0, multi = 1;

foreach (var c in input.Reverse())
{
decoded += multi * alphabet.IndexOf(c);
multi *= alphabet.Length;
}

return decoded;
}

blog.daruyanagi.net/archives/264

I made NuGet package to embed a photo on Flickr to your homepage. Please enjoy !

Flickr2Html nuget.org/List/Packages/Flickr2Html

@Flickr2Html.GetHtml5("http://flic.kr/p/asHoxQ")
Posted 8 months ago. (permalink)

view photostream

tarmo888  Pro User  says:

Would be nice if these 2 formats would be added to shorturl too.
www.flickr.com/groups/api/discuss/72157629196782290/
Posted 2 months ago. (permalink)

Would you like to comment?

Sign up for a free account, or sign in (if you're already a member).

RSS 2.0 feedSubscribe to a feed of stuff on this page...</!!> Feed – Subscribe to Flickr API discussion threads