tumblr / pytumblr

A Python Tumblr API v2 Client
Apache License 2.0
723 stars 196 forks source link

I can't iterate over the list of dictionaries gotten from the api, please help #107

Closed DrunkCodes closed 6 years ago

DrunkCodes commented 6 years ago

This is how my code looks tumblr issue posts = client.tagged('basketball', limit=10, filter='text') # get posts with this tags

for index in range(len(post)): for key in posts[index]: for keys in posts[index]: if key is 'id': if keys is 'reblog_key': post_id = posts[index][key] reblog_key = posts[index][keys] client.reblog('emmanuel74.tumblr.com', id=post_id, reblog_key=reblog_key)

The IDEA didn't show any error and it ran fine with zero code but it wasn't able to pass the post id and it's reblog_key to the client.reblog and even when I tried to print it after the reblog_key identation it could not.

please can someone help me fix it. Thanks.

jasonpenny commented 6 years ago

There's a few things here.

The first is the use of if key is 'id', you generally should not compare strings with is. Only strings that have been interned will return True with is, so use == instead. So this will print out some results:

 for index in range(len(posts)):
     for key in posts[index]:
         for keys in posts[index]:
             if key == 'id':
                 if keys == 'reblog_key':
                     post_id = posts[index][key]
                     reblog_key = posts[index][keys]
                     # client.reblog('emmanuel74.tumblr.com', id=post_id, reblog_key=reblog_key)
                     print 'reblog', post_id, reblog_key

but, this code is looping over the posts[index] keys recursively without a need to. This code is basically equivalent:

for post in posts:
    if 'id' in post and 'reblog_key' in post:
        post_id = post['id']
        reblog_key = post['reblog_key']

        # client.reblog('emmanuel74.tumblr.com', id=post_id, reblog_key=reblog_key)
        print 'reblog', post_id, reblog_key



and just as a note, instead of including a screenshot of your code, you can wrap the code in triple-backticks to have github display it with syntax highlighting:

```python
code goes here
DrunkCodes commented 6 years ago

Thank you very much. You really made my day. @jasonpenny Can I be your friend please?

Am working on a desktop tumblr app.