# this script is a modification of https://github.com/simondlr/Python-App.net-API-Wrapper/blob/master/appdotnet/appdotnet.py
# An App.net developer account is required to generate your auth token.
# Go to account.app.net/developers/apps
# Select 'Create an App'
# App name, website, callback can be any values
# When the app has been created, go to App Settings, and 'Generate a user token for yourself'
# Enter the user token below.
auth_token = 'enter your user token here'
'''
MIT License
Copyright (c) 2012 Simon de la Rouviere
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
'''
#coding: utf-8
import json
import requests
import workflow
post_text = workflow.get_input()
class appdotnet:
def __init__(self, client_id=None, client_secret=None, redirect_uri=None,
scope=None, access_token=None):
#for server authentication flow
self.client_id = client_id
self.client_secret = client_secret
self.redirect_uri = redirect_uri
self.scope = scope
self.access_token = access_token
self.api_anchor = "alpha.app.net" #for when the versions change
#anchors currently different
self.public_api_anchor = "alpha-api.app.net"
#scopes provided by app.net API
self.allowed_scopes = ['stream', 'email', 'write_post',
'follow', 'messages','export']
#GET REQUESTS
def getRequest(self, url, getParameters=None):
if not getParameters:
getParameters = {}
#access token
url = url + "?access_token=" + self.access_token
#if there are any extra get parameters aside from the access_token, append to the url
if getParameters != {}:
for key,value in getParameters.iteritems():
url = url + "&" + key + "=" + value
r = requests.get(url)
return r.text
#POST REQUESTS
def postRequest(self, url, data=None, headers=None):
if not data:
data = {}
if not headers:
headers = {}
headers['Authorization'] = 'Bearer %s' % self.access_token
url = url
r = requests.post(url,data=json.dumps(data),headers=headers)
return r.text
'''
POSTS: http://developers.app.net/docs/resources/post/
'''
def createPost(self, text, reply_to = None, annotations=None, links=None):
url = "https://%s/stream/0/posts" % self.public_api_anchor
if annotations != None:
url = url + "?include_annotations=1"
data = {'text':text}
if reply_to != None:
data['reply_to'] = reply_to
if annotations != None:
data['annotations'] = annotations
if links != None:
data['links'] = links
return self.postRequest(url,data,headers={'content-type':'application/json'})
api = appdotnet(access_token=auth_token)
post = api.createPost(post_text)
There are no comments yet.