Tumblr Python storing of post photos urls from JSON

261 Views Asked by At

I am trying to figure out how do I store multiple url links into a python array key or any other methods as long as i can store the multiple url links.

In the data im using, every post may or may not contain multiple 'photos' image object (in JSON) so I want to store every post image objects.

E.g. of data from https://www.tumblr.com/docs/en/api/v2

"posts": [
         {
            "blog_name": "derekg",
            "id": 7431599279,
            "post_url": "http:\/\/derekg.org\/post\/7431599279",
            "type": "photo",
            "date": "2011-07-09 22:09:47 GMT",
            "timestamp": 1310249387,
            "format": "html",
            "reblog_key": "749amggU",
            "tags": [],
            "note_count": 18,
            "caption": "<p>my arm is getting tired.<\/p>",
            "photos": [
               {
                  "caption": "",
                  "alt_sizes": [
                     {
                        "width": 1280,
                        "height": 722,
                        "url": "http:\/\/derekg.org\/photo\/1280\/7431599279\/1\/
                           tumblr_lo36wbWqqq1qanqww"
                     },
                     {
                        "width": 500,
                        "height": 282,
                        "url": "http:\/\/30.media.tumblr.com\/
                           tumblr_lo36wbWqqq1qanqwwo1_500.jpg"
                     },
                     {
                        "width": 400,
                        "height": 225,
                        "url": "http:\/\/29.media.tumblr.com\/
                           tumblr_lo36wbWqqq1qanqwwo1_400.jpg"
                     },
                     {
                        "width": 250,
                        "height": 141,
                        "url": "http:\/\/26.media.tumblr.com\/
                           tumblr_lo36wbWqqq1qanqwwo1_250.jpg"
                     },
                     {
                        "width": 100,
                        "height": 56,
                        "url": "http:\/\/24.media.tumblr.com\/
                           tumblr_lo36wbWqqq1qanqwwo1_100.jpg"
                     },
                     {
                        "width": 75,
                        "height": 75,
                        "url": "http:\/\/30.media.tumblr.com\/
                           tumblr_lo36wbWqqq1qanqwwo1_75sq.jpg"
                     }
                  ]
               }
            ]
         }
      ]

An my python so far:

raw_json_data = requests.get('api.tumblr.com/v2/blog/{blog-identifier}/likes?api_key={key}')
data = raw_json_data.json()
data_format = data['response']['liked_posts']

number = 0

dat = [{} for i in range(len(data['response']['liked_posts']))]

for posts in data_format:
    #print(posts['blog_name'])
    #print(posts['timestamp'])
    g = 0
    dat[number]['blog_name'] = posts['blog_name']
    dat[number]['tags'] = posts['tags']
    dat[number]['timestamp'] = posts['timestamp']

    if len(posts['photos']) > 1:
    dat[number]['url'] = {}
    g = 0
    for g, u in range(len(posts['photos'])):
        dat[number]['url'][g] = u['alt_sizes'][0]['url']
        g += 1

    number += 1
with open(json_storage, 'w') as outputFile:
    json.dump(dat, outputFile)

I am receiving an error now that its still not storing into my JSON file and the key 'url' is now missing from all posts

1

There are 1 best solutions below

0
On

I don't see a minimal working example (I need to see JSON parsing), but it looks to me like you're trying to create multiple layers of dictionary keys in one statement. Python only allows you to initialize one key/index at a time, due to reliance on __setitem__. So, you'll need to initialize ...['url'] before you can assign to ...['url'][g].

dat[number]['url'][g] = ... can also be thought of as ((dat[number])['url'])[g] = .... You should now be able to see the two index reads that you're trying to do before the assignment can occur.

Partial example (psuedo-code!) using a dictionary comprehension:

for posts in JSON_DATA:
    #print(posts['blog_name'])
    #print(posts['timestamp'])

    dat[number]['blog_name'] = posts['blog_name']
    dat[number]['tags'] = posts['tags']
    dat[number]['timestamp'] = posts['timestamp']
    g = 0
    if len(posts['photos']) > 1:
        dat[number]['url'] = {k:v['alt_sizes'][0]['url'] for (k, v) in enumerate(posts['photos'])}

Alternate example (more psuedo-code) using mostly the same for loop:

for posts in JSON_DATA:
    #print(posts['blog_name'])
    #print(posts['timestamp'])

    dat[number]['blog_name'] = posts['blog_name']
    dat[number]['tags'] = posts['tags']
    dat[number]['timestamp'] = posts['timestamp']
    g = 0
    if len(posts['photos']) > 1:
        dat[number]['url'] = {}
        for g, u in enumerate(posts['photos']):
            dat[number]['url'][g] = u['alt_sizes'][0]['url']
    number += 1