1

I am using python-instagram API and I am displaying some images with searched tag!

Rather than displaying, I want to save those images so that it can be used for further analysis.

Is it possible? I am new to python and using API's.

Here is my code snippet which does this:

@route('/tag_search')
def tag_search(session): 
    access_token = session.get('access_token')
    content = "<h2>Tag Search</h2>"
    if not access_token:
        return 'Missing Access Token'
    try:
        api = client.InstagramAPI(access_token=access_token)
        tag_search, next_tag = api.tag_search(q="catband")
        tag_recent_media, next = api.tag_recent_media(tag_name=tag_search[0].name)
        photos = []
        for tag_media in tag_recent_media:
            photos.append('<img src="%s"/>' % tag_media.get_standard_resolution_url())
        content += ''.join(photos)
    except Exception, e:
        print e     

Thanx in advance:)

twasbrillig
  • 17,084
  • 9
  • 43
  • 67
Jay Patel
  • 1,266
  • 6
  • 20
  • 38

1 Answers1

3

After some help from comments, and other resources, I found out that since I have URL of the image, I can use it to download!

The library which is used was "urllib" I used a counter variable to save images in the same directory where the file is and in the form of 1.jpg, 2.jpg and so on and so forth.

Here is the modified code:

@route('/tag_search')
def tag_search(session): 
access_token = session.get('access_token')
content = "<h2>Tag Search</h2>"
if not access_token:
    return 'Missing Access Token'
try:
    api = client.InstagramAPI(access_token=access_token)
    tag_search, next_tag = api.tag_search(q="selfie")
    tag_recent_media, next = api.tag_recent_media(tag_name=tag_search[0].name)
    photos = []
    count = 0
    for tag_media in tag_recent_media:
        photos.append('<img src="%s"/>' % tag_media.get_standard_resolution_url())
        urllib.urlretrieve(tag_media.get_standard_resolution_url(), `count`+".jpg")
        count = count + 1
    content += ''.join(photos)
except Exception, e:
    print e              

Hope this Helps:)

Jay Patel
  • 1,266
  • 6
  • 20
  • 38