How to get static image url from flickr URL?

前端 未结 7 1359
长情又很酷
长情又很酷 2020-12-24 01:40

Is it possible to get static image URL from the flickr URL via an api call or some script ?
For eg :
Flickr URL -> http://www.flickr.com/photos/53067560@N00/26581

相关标签:
7条回答
  • 2020-12-24 02:04

    Here's some code I wrote to retrieve metadata from a Flickr Photo based on its ID:

    I first defined a javascript object FlickrPhoto to hold the photo's metadata:

    function FlickrPhoto(title, owner, flickrURL, imageURL) {
        this.title = title;
        this.owner = owner;
        this.flickrURL = flickrURL;
        this.imageURL = imageURL;
    }
    

    I then created a FlickrService object to hold my Flickr API Key and all my ajax calls to the RESTful API.

    The getPhotoInfo function takes the Photo ID as parameter, constructs the appropriate ajax call and passes a FlickrPhoto object containing the photo metadata to a callback function.

    function FlickrService() {
        this.flickrApiKey = "763559574f01aba248683d2c09e3f701";
        this.flickrGetInfoURL = "https://api.flickr.com/services/rest/?method=flickr.photos.getInfo&nojsoncallback=1&format=json";
    
        this.getPhotoInfo = function(photoId, callback) {
            var ajaxOptions = {
                type: 'GET',
                url: this.flickrGetInfoURL,
                data: { api_key: this.flickrApiKey, photo_id: photoId },
                dataType: 'json',
                success: function (data) { 
                    if (data.stat == "ok") {
                        var photo = data.photo;
                        var photoTitle = photo.title._content;
                        var photoOwner = photo.owner.realname;
                        var photoWebURL = photo.urls.url[0]._content;
                        var photoStaticURL = "https://farm" + photo.farm + ".staticflickr.com/" +  photo.server + "/" + photo.id + "_" + photo.secret + "_b.jpg";
    
                        var flickrPhoto = new FlickrPhoto(photoTitle, photoOwner, photoWebURL, photoStaticURL);
                        callback(flickrPhoto);
                    }
                }
            };
    
            $.ajax(ajaxOptions);
        }
    }
    

    You can then use the service as follows:

    var photoId = "11837138576";
    var flickrService = new FlickrService();
    flickrService.getPhotoInfo(photoId, function(photo) {
        console.log(photo.imageURL);
        console.log(photo.owner);
    });
    

    Hope it helps.

    0 讨论(0)
  • 2020-12-24 02:06

    Below a solution without using flickr-apis, only standard Linux commands (actually I ran it on MS Windows with Cygwin):

    • Put your list of URLs in the tmp variable
    • If you are downloading private photos like me, the protocol will be https and you'll need to pass the authentication cookies to wget. I log on with a browser (Chrome) and exported the cookies file using an extension
    • If you access public URLs, just remove the parameter --load-cookies $cookies
    • The script downloads in the local folder the photos in their original format
    • If you want just the URL of the static image, remove the last command | xargs wget --load-cookies $cookies

    Here the script, you can use it as a start for your explorations:

    cookies=~/cookies.txt
    root="https://www.flickr.com/photos/131469243@N02/"
    
    tmp="https://www.flickr.com/photos/131469243@N02/29765108124/in/album-72157673986011342/
    https://www.flickr.com/photos/131469243@N02/29765103724/in/album-72157673986011342/
    https://www.flickr.com/photos/131469243@N02/29765102344/in/album-72157673986011342/"
    
    while read -r url; do
    
        if  [[ $url == http* ]] ;
        then
            url2=$root`echo -n $url | grep -oP '(?<=https://www.flickr.com/photos/131469243@N02/)\w+'`/sizes/o
            wget -q --load-cookies $cookies -O - $url2 | grep -io 'https://c[0-9].staticflickr.com.*_o_d.jpg'  | xargs wget --load-cookies $cookies
        fi
    done <<< "$tmp";
    
    0 讨论(0)
  • 2020-12-24 02:15

    Get it from the html code of the image. :)

    Parse the atribute style and get your required Static URL :)

    page_soup = soup(page_html,"lxml")
    all_links = page_soup.find("div",{"class":"view photo-list-view requiredToShowOnServer photostream"}).findAll("div",{"class":"view photo-list-photo-view requiredToShowOnServer photostream awake"})
    for link in all_links:
        image_url = link["style"].split("//")[1].replace('");','')
        print(image_url)
    

    The above section of Python code will print out all the images url needed available on Photostream tab in Flickr.

    0 讨论(0)
  • 2020-12-24 02:16

    You can also access the original image using the photoId (number before the first underscore)

    http://flickr.com/photo.gne?id=photoId

    In your case it would be:

    https://www.flickr.com/photo.gne?id=2658147888

    0 讨论(0)
  • 2020-12-24 02:17

    With specifying extras=url_o you get a link to the original image:

    https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=YOURAPIKEY&format=json&nojsoncallback=1&text=cats&extras=url_o
    

    For downscaled images, you use the following parameters: url_t, url_s, url_q, url_m, url_n, url_z, url_c, url_l

    Alternatively, you can construct the URL as described:

    http://farm{farm-id}.staticflickr.com/{server-id}/{id}_{secret}.jpg
    or
    http://farm{farm-id}.staticflickr.com/{server-id}/{id}_{secret}_[mstzb].jpg
    
    0 讨论(0)
  • 2020-12-24 02:22

    Not sure if you can get it directly through a single API call, but this link explains how the urls for the images are contructed: link

    0 讨论(0)
提交回复
热议问题