tumblr / pytumblr

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

Support for "answer" posts #79

Open jonfortescue opened 8 years ago

jonfortescue commented 8 years ago

Currently there is no support for answer posts. While answer posts cannot be created, they can be edited.

I tried adding the functionality myself, but ran into issues with pytumblr recognizing answer posts. Any ideas?

Macman1234 commented 8 years ago

Do you mean creating answers to questions asked? If so, the way to do that through the tumblr API is to, through editing it, setting the "answer" tag to whatever you want to answer (which requires the changing of the valid parameters in the code), and setting the state from "submission" to "published"

jonfortescue commented 8 years ago

The method _post_valid_options doesn't have any valid options for answer posts (see https://github.com/tumblr/pytumblr/blob/master/pytumblr/__init__.py#L469). Last time I tried working on this (which admittedly was a while ago), I thought it was more complicated than simply adding an if-statement to this one method... if I have time this weekend, I'll see if I can dig up what I did to get answer support. IIRC, though, it was fairly kludgey, which is why I opened an issue instead of making a pull request.

Macman1234 commented 8 years ago

Sorry about not responding earlier, but I got that to work simply by changing

valid = ['type', 'state', 'tags', 'tweet', 'date', 'format', 'slug']

to

valid = ['type', 'state', 'tags', 'tweet', 'date', 'format', 'slug', 'answer']

then used

asklist = client.submission('[my blog's name]')

for ask in asklist['posts']:
    if ask['type'] == 'answer':
        if ask['state'] == 'submission':
            client.edit_post('[my blog's name]', id=ask['id'], answer=[whatever I want my bot to answer to any given ask], state='published');

to answer every ask that hasn't yet been answered.

sorry if that was weirdly ambiguous with the brackets.

Macman1234 commented 8 years ago

However, if you mean submitting answer posts to other blogs using pytumblr, I have no idea whether that is possible or how to do that if it is.

sintelligentdesign commented 8 years ago

@Macman1234 I may have a slight improvement on your solution for answering questions: instead of adding 'answer' to the list of valid options for all post types, what about adding elif post_type == 'answer': valid += ['answer'] near the bottom of _post_valid_options() where all the other if statements are, and then answer asks using client.edit_post('[blog name]', id=[ask ID], type='answer', answer=[answer], state='published') (note the inclusion of a 'type' arg)?

jonfortescue commented 8 years ago

@sintelligentdesign That looks great to me. Have you done any testing with that solution?

sintelligentdesign commented 8 years ago

I've been using it in one of my projects and it's been working reliably. That said, I haven't written any tests for it. It's a minor change but that's still probably important (I'm just garbage at writing tests).

Macman1234 commented 8 years ago

@sintelligentdesign That's almost certainly a better way of implementing that then the way that I've been using: a lot more sane to prevent using "answer" in editing other types of posts. Thanks for that, I'll probably switch over to using it that way.

bratchan commented 6 years ago

For answering the ask, is there a way to use an image with it?

sintelligentdesign commented 6 years ago

You can include an HTML <img> element in your answer field, which appears to work on desktop (but not mobile?), unless the post is set to a draft before being published, in which case the image seems to disappear (??). I can try to see if I can figure out something better (read: reliable) sometime this week.

bratchan commented 6 years ago

Cool

bratchan commented 6 years ago

Any luck for images? I know that when i answer one it auto uploads it. I was hoping not to host the images and let tumbrl handle them like i have been with answers.

sintelligentdesign commented 6 years ago

By "when I answer one it auto uploads it," are you talking about using the website and answering questions with images manually, or using the API? I assume the first but i just wanted to be sure.

According to the API docs, answer posts don't support the data field used in image posts, and when I gave editing an answer post by adding a data field a shot, it didn't return an error but it didn't work either.

One possibility, although you mentioned wanting to avoid hosting images outside of Tumblr, could be to use an image hosting service's API (I initially was going to suggest Imgur but when I looked the Imgur Python API was deprecated, bummer) and adding the link to the image in the answer field of the answer post. That still has the issue though of apparently not working on mobile (Maybe? Can anybody else test this? Maybe I'm just doing something wrong and dumb. I haven't tried Markdown as the post format but I can't imagine it would make a difference).