Created
March 6, 2016 19:08
-
-
Save dmil/21108a5b348d34771447 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# | |
# Ruby script to access wordpress.com REST API | |
# For usage, see main method at end of file. | |
require 'wordpress-com' | |
require 'json' | |
require 'logger' | |
@logger = Logger.new(STDOUT) | |
@logger.level = Logger::INFO | |
# User can run to get credentials (wpc object) | |
def get_credentials | |
redirect_uri = 'http://538.io' | |
puts "\nEnter your blog name (examples: fivethirtyeight.com, dhrumilmehtablog.wordpress.com)" | |
blog = gets.chomp | |
puts "\nEnter your client_id (https://developer.wordpress.com/apps/)" | |
client_id = gets.chomp | |
puts "\nEnter your client secret (https://developer.wordpress.com/apps/)" | |
client_secret = gets.chomp | |
# Create wpc Object | |
wpc = WordpressCom.new(client_id, client_secret) | |
# Manually get an Oauth2 Code from URL | |
puts "\n#{wpc.authorize_url(redirect_uri, :blog => blog)}" | |
puts "Go to URL, Authorize, Enter Code Below:" | |
code = gets.chomp | |
# Use code to get an auth token that never expires | |
wpc.get_token(code, redirect_uri) | |
return wpc # Reuse this object every time you want to use the API | |
end | |
# Save wpc object to keys.json | |
def save_to_keys(wpc) | |
keyfile = File.read('keys.json') | |
keys = keyfile.empty? ? Hash.new : JSON.parse(keyfile) | |
keys["wordpress"] = wpc.serialize | |
File.open('keys.json', 'w') { |f| f.write(JSON.generate(keys)) } | |
@logger.info "wpc object sucessfully saved to keys.json" | |
rescue | |
@logger.fatal "Your keys.json file may not exist or may be corrupt. `touch keys.json` or import it from elsewhere and re-run this program. Exiting." | |
raise | |
end | |
# Get wpc object from keys.json | |
def get_wpc | |
begin | |
keyhash = JSON.parse(File.read('keys.json'))["wordpress"] | |
rescue | |
@logger.fatal "Your keys.json file doesn't exist, is corrupt, or doesn't contain a wordpress credentials object. Exiting."; raise | |
end | |
WordpressCom.deserialize(keyhash) | |
end | |
# Get a wordpress post. | |
# Click on edit post in wordpress dashboard UI to get post_number | |
def get_post(wpc, post_number) | |
wpc.get("posts/#{post_number}") | |
end | |
# Edit a post. Will only edit fields in post_body_hash | |
# that are included. Does not override other fields in | |
# the existing post. | |
# | |
# Example of post_body_hash below | |
# { | |
# :title => "Hello, World!", | |
# :content => "Lorem ", | |
# :tags => 'foo,bar,xyzzy' | |
# } | |
# | |
# Aditional Options : https://developer.wordpress.com/docs/api/1/post/sites/%24site/posts/%24post_ID/ | |
def edit_post(wpc, post_number, post_body_hash) | |
wpc.post("posts/#{post_number}", :body => post_body_hash) | |
end | |
# Create a new post. | |
# | |
# Example of post_body_hash below | |
# { | |
# :title => "Hello, World!", | |
# :content => "Lorem ", | |
# :tags => 'foo,bar,xyzzy' | |
# } | |
# | |
# Additional Options : https://developer.wordpress.com/docs/api/1/post/sites/%24site/posts/new/ | |
def new_post(wpc, post_body_hash) | |
wpc.post("posts/new", :body => post_body_hash) | |
end | |
if __FILE__ == $PROGRAM_NAME | |
## | |
# First time: Run these lines to retrieve and save credentials | |
## | |
# wpc = get_credentials | |
# save_to_keys(wpc) | |
## | |
# Subsequent times: Here is sample usage code to upload a new post | |
## | |
# wpc = get_wpc | |
# test_post_body = { | |
# :title => "Testing Programmatic Upload!", | |
# :content => "Lorem ", | |
# :tags => 'foo,bar,xyzzy' | |
# } | |
# new_post(wpc, test_post_body) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment