An unofficial Write.as API client library for Python.
pip install writeasapi
Importing and Instanstiating: Each request to the API will be made through an instance of the NewClient class. You can instantiate in two ways:
import writeas c = writeas.NewClient()
from writeas import NewClient c = NewClient() # It is up to you really! This way may also be best for saving memory in a project that you aren't simply running locally
Logging in and Setting Token: Make sure to login and set the token, otherwise certain authorized requests will not be possible.
from writeas import NewClient c = NewClient() c.login("username", "password") # This prints out the user data which includes the access token # We will store the token into the client instance for authenticated requests c.setToken("00000000-0000-0000-0000-000000000000") # Prints out the token and you are set to use the API!
Logging Out: To log out, all you need is to put the access token in as an argument
c.logout("00000000-0000-0000-0000-000000000000") # Returns a 204 status code # The access token shouldn't work in a future request after logging out
Creating a post:
p = c.createPost("This is a the body of the post.", "This is a Title") print p # This will return the post's data when successful # That data includes the post token which we'll need for doing cooling stuff with the post
Updating a Post: All you need is the post's id, the post's token, and the part of the post you want to edit. For now it is set to edit the post body only.
update = c.updatePost('7qmni7cpg5sjks11', '123456789abcdefgthisisfakeposttoken', 'I am updating this post's body!') print update # This will return the post's updated data when successful
Deleting a Post: Since you are logged in, all you need is the post's id.
delete = c.deletePost('7qmni7cpg5sjks11') print delete # This will return a 'None', meaning that it worked
Retrieving a Post: For finding a post, all you need is the post's id.
post = c.retrievePost('7qmni7cpg5sjks11') print post # This will return the post's data
Claiming a Post: If you create an anonymous post but want to claim it, all you need is the post's id and token
post = c.claimPost('7qmni7cpg5sjks11', '123456789abcdefgthisisfakeposttoken') print post # This will return the post's data
Creating a collection: All you need is a collection alias and title.
collection = c.createCollection('collectionalias', 'My Cool Blog') print collection # This will return the collection's data # Keep the collection's alias at hand: it will be a token for making requests with the collection
Retrieving a Collection: All you need is the collection's alias.
collection = c.retrieveCollection('collectionalias') pritnt collection # This will return the collection's data
Deleting a Collection: All you need is the collection's alias.
delete = c.deleteCollection('collectionalias') print delete # This will return a 'None', meaning it was successfully deleted
Retrieve a Collection Post: To retrieve a collection post, all you need is the collection's alias and the post's slug. Say we want to grab this post: https://write.as/matt/stepping-back
post = c.retrieveCPost('matt', 'stepping-back') print post # This will return the post's data
Retrieve a Collection's Posts: To get a collection's posts, all you need is the collection's alias. So if I wanted to grab all the posts from here: https://write.as/matt/
posts = c.retrieveCPosts('matt') print posts # This will return data from all the collection's posts
me = c.retrieveUser() print me # Returns your user info
Retrieve User Posts:
myPosts = c.retrievePosts() print myPosts # Returns your posts with this user account
Retrieve User Collections:
myCollections = c.retrieveCollections() print myCollections # Returns your collections
Retrieve User Channels:
myChannels = c.retrieveChannels() print myChannels # Returns the channels you send your posts to (Tumblr, Medium, etc)