Frage

I'm extremely new to coding in general; I delved into this project in order to help my friend tag her fifteen thousand and some-odd posts on Tumblr. We've finally finished, but she wants to be sure that we haven't missed anything... So, I've scoured the internet, trying to find a coding solution. I came across a script found here, that allegedly does exactly what we need -- so I downloaded Python, and...It doesn't work.

More specifically, when I click on the script, a black box appears for about half a second and then disappears. I haven't been able to screenshot the box to find out exactly what it says, but I believe it says there's a syntax error. At first, I tried with Python 2.4; it didn't seem to find the Json module the creator uses, so I switched to Python 3.3 -- the most recent version for Windows, and this is where the Syntax errors occur.

#!/usr/bin/python

import urllib2
import json

hostname = "(Redacted for Privacy)"
api_key = "(Redacted for Privacy)"

url = "http://api.tumblr.com/v2/blog/" + hostname + "/posts?api_key=" + api_key

def api_response(url):
req = urllib2.urlopen(url)
return json.loads(req.read())

jsonresponse = api_response(url)
post_count = jsonresponse["response"]["total_posts"]
increments = (post_count + 20) / 20

for i in range(0, increments):

  jsonresponse = api_response(url + "&offset=" + str((i * 20)))
  posts = jsonresponse["response"]["posts"]

for i in range(0, len(posts)):
 if not posts[i]["tags"]:
  print posts[i]["post_url"]

print("All finished!")

So, uhm, my question is this: If this coding has a syntax error that could be fixed and then used to find the Untagged Posts on Tumblr, what might that error be? If this code is outdated (either via Tumblr or via Python updates), then might someone with a little free time be willing to help create a new script to find Untagged posts on Tumblr? Searching Tumblr, this seems to be a semi-common problem.

In case it matters, Python is installed in C:\Python33.

Thank you for your assistance.

War es hilfreich?

Lösung

when I click on the script, a black box appears for about half a second and then disappears

At the very least, you should be able to run a Python script from the command line e.g., do Exercise 0 from "Learn Python The Hard Way".


"Finding Untagged Posts on Tumblr" blog post contains Python 2 script (look at import urllib2 in the source. urllib2 is renamed to urllib.request in Python 3). It is easy to port the script to Python 3:

#!/usr/bin/env python3
"""Find untagged tumblr posts.

Python 3 port of the script from
  http://www.alexwlchan.net/2013/08/untagged-tumblr-posts/
"""
import json
from itertools import count
from urllib.request import urlopen

hostname, api_key = "(Redacted for Privacy)", "(Redacted for Privacy)"
url = "https://api.tumblr.com/v2/blog/{blog}/posts?api_key={key}".format(
    blog=hostname, key=api_key)

for offset in count(step=20):
    r = json.loads(urlopen(url + "&offset=" + str(offset)).read().decode())
    posts = r["response"]["posts"]
    if not posts: # no more posts
        break
    for post in posts:
        if not post["tags"]: # no tags
            print(post["post_url"])

Here's the same functionality implemented using the official Python Tumblr API v2 Client (Python 2 only library):

#!/usr/bin/env python
from itertools import count
import pytumblr # $ pip install pytumblr

hostname, api_key = "(Redacted for Privacy)", "(Redacted for Privacy)"
client = pytumblr.TumblrRestClient(api_key, host="https://api.tumblr.com")
for offset in count(step=20):
    posts = client.posts(hostname, offset=offset)["posts"]
    if not posts: # no more posts
        break
    for post in posts:
        if not post["tags"]: # no tags
            print(post["post_url"])

Andere Tipps

Tumblr has an API. You probably would have much better success using it.

https://code.google.com/p/python-tumblr/

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top