maxcutler / python-wordpress-xmlrpc

Python library for WordPress XML-RPC integration
http://python-wordpress-xmlrpc.rtfd.org
MIT License
378 stars 129 forks source link

Inplace post edits are returning Fault 404: 'Invalid attachment ID.' #29

Open felciano opened 12 years ago

felciano commented 12 years ago

I am trying to use your library (v2.1) to write a script to replace broken links after a blog migration to Wordpress.com. After retrieving posts and making corrections to the post.body, I

if not(wp.call(EditPost(post.id, post))):
    logging.error("Unable to edit post %s" % (post.id))
else:
    logging.info("Successfully updated post %s" % (post.id))

Running this consistently yields this error, irrespective of the post in question or whether any changes were made:

Traceback (most recent call last):
  File "wplinkrepair.py", line 260, in <module>
    fix_errors=args.repair)
  File "wplinkrepair.py", line 206, in process_broken_links
    if not(wp.call(EditPost(post.id, post))):
  File "C:\Python27\lib\site-packages\wordpress_xmlrpc\base.py", line 37, in call
    raw_result = server_method(*args)
  File "C:\Python27\lib\xmlrpclib.py", line 1224, in __call__
    return self.__send(self.__name, args)
  File "C:\Python27\lib\xmlrpclib.py", line 1575, in __request
    verbose=self.__verbose
  File "C:\Python27\lib\xmlrpclib.py", line 1264, in request
    return self.single_request(host, handler, request_body, verbose)
  File "C:\Python27\lib\xmlrpclib.py", line 1297, in single_request
    return self.parse_response(response)
  File "C:\Python27\lib\xmlrpclib.py", line 1473, in parse_response
    return u.close()
  File "C:\Python27\lib\xmlrpclib.py", line 793, in close
    raise Fault(**self._stack[0]) 
xmlrpclib.Fault: <Fault 404: 'Invalid attachment ID.'>```

Any suggestions?

felciano commented 12 years ago

I should clarify that part of the link rewriting is for embedded images, which is maybe what the "attachment ID" refers to. I've verified that these media are in the wordpress.com site and the URLs are correct. Is there any way to list out the "attachment IDs" that a post refers to?

maxcutler commented 12 years ago

WordPress gives more information in GetPost than it expects in EditPost, which is leading to this issue. I can handle it better in the library, and I'll try to do so in the next release.

In the meantime, you can try calling post.thumbnail = post.thumbnail['attachment_id'] before EditPost. This will change it to the value expected by WordPress. Let me know if that doesn't work and I can dig deeper.

felciano commented 12 years ago

No luck -- still dumps out the same error. FWIW here is what post.thumbnail has before the assignment:

{
    'attachment_id': '720',
    'description': '', 
    'parent': 716, 
    'title': 'Chicco-KeyFit-Infant-Car-Seat-Limonata', 
    'date_created_gmt': <DateTime '20120521T19:15:47' at 2edaf80>, 
    'caption': '', 
    'link': 'http://minimalistmamatest.files.wordpress.com/2012/05/chicco-keyfit-infant-car-seat-limonata.jpg', 
    'thumbnail': 'http://minimalistmamatest.files.wordpress.com/2012/05/chicco-keyfit-infant-car-seat-limonata.jpg?w=150', 
    'metadata': {
        'width': '500', 
        'hwstring_small': "height='96' width='106'", 
        'image_meta': {'shutter_speed': '0', 
        'copyright': '', 
        'caption': '', 
        'title': '', 
        'credit': '', 
        'created_timestamp': '0', 
        'camera': '', 
        'iso': '0', 
        'focal_length': '0', 
        'aperture': '0'
    }, 
    'file': '/home/wpcom/public_html/wp-content/blogs.dir/606/36648499/files/2012/05/chicco-keyfit-infant-car-seat-limonata.jpg', 
    'height': '451'
}

Are you intending to overwrite this dictionary with just the value of the attachment_id field (i.e. '720')?

maxcutler commented 12 years ago

Correct, thumbnail for EditPost should just be the ID. Can you confirm in WordPress that a media item with ID=720 actually exists?

Unfortunately I won't have time to investigate this further until next week. Another alternative could be to just unset the thumbnail field (post.thumbnail = None); WordPress should leave the existing thumbnail alone in that case.

felciano commented 12 years ago

I think the media looks right, although I'm not very familiar with that part of the API. The following prints the right name for the thumbnail ("Chicco-KeyFit-Infant-Car-Seat-Limonata"):

print post.thumbnail
print wp.call(GetMediaItem(post.thumbnail["attachment_id"]))

Part of the purpose of this script is to update links within posts that point to--or display--image media. The posts were exported from blog A and imported to blog B. Both blogs contain the necessary media, but the export/import process didn't update the links so the posts in blog B still display images from blog A. So I'm basically just renaming the links to point to the blog B media directory.

I'm not sure I understand how and when thumbnails are generated. Based on the above, does post.thumbnail = None seem like a safe operation?

evandavey commented 11 years ago

+1 on this issue

Triggered when calling EditPost on a post where a thumbnail exists. Setting post.thumbnail=post.thumbnail['attachment_id'] doesn't help.

picklepete commented 11 years ago

@evandavey @felciano Hi guys, were either of you experiencing this issue in conjunction with @maxcutler's WordPress plugin?

felciano commented 11 years ago

Nope, this was all on Wordpress.com (hosted), so no special plugins.

picklepete commented 11 years ago

@felciano @evandavey I believe this is the issue, I've attached a patch if you are still getting the "Invalid attachment ID" error.

paulakg4 commented 10 years ago

just running into the same issue and the solution:

post.thumbnail = post.thumbnail['attachment_id'] before EditPost

did in fact fix the problem for me - Thanks!

zodman commented 8 years ago

+1 to fix from the code!

swanithakvn commented 7 years ago

Thank you! using post.thumbnail = post.thumbnail['attachment_id'] helped!!