Skip to content

Instantly share code, notes, and snippets.

@widnyana
Created August 18, 2015 08:34
Show Gist options
  • Save widnyana/d3108597701bb4d9b2d9 to your computer and use it in GitHub Desktop.
Save widnyana/d3108597701bb4d9b2d9 to your computer and use it in GitHub Desktop.
Demonstrating Sebangsa OpenAPI
#!/usr/bin/env python
# -*- Coding: utf-8 -*-
from json import dumps
from urllib import urlencode, unquote
from urlparse import urlparse, parse_qsl, ParseResult
from requests_oauthlib import OAuth1Session
base_url = "put base url here"
sess = OAuth1Session('CLIENT_KEY',
client_secret='CLIENT_SECRET',
resource_owner_key='ACCESS_TOKEN',
resource_owner_secret='ACCESS_TOKEN_SECRET')
def add_url_params(url, params):
"""taken from: http://stackoverflow.com/a/25580545/"""
url = unquote(url)
parsed_url = urlparse(url)
get_args = parsed_url.query
parsed_get_args = dict(parse_qsl(get_args))
parsed_get_args.update(params)
parsed_get_args.update(
{k: dumps(v) for k, v in parsed_get_args.items()
if isinstance(v, (bool, dict))}
)
encoded_get_args = urlencode(parsed_get_args, doseq=True)
new_url = ParseResult(
parsed_url.scheme, parsed_url.netloc, parsed_url.path,
parsed_url.params, encoded_get_args, parsed_url.fragment
).geturl()
return new_url
def create_post():
status = raw_input("Masukkan text: ").strip()
post_payload = {
"text": status,
"location_long": 109.918425099999920000, #: sample
"location_lat": 7.203125799999999000, #: sample
"location_name": "Dataran Tinggi Dieng", #: sample
"location_address": "Jawa Tengah" #: sample
}
url = base_url + "/post"
data = sess.post(url, post_payload)
print(data.text)
def get_location():
keyword = raw_input("Masukkan keyword: ").strip()
endpoint = base_url + "/spot"
payload = {
"q": keyword,
}
url = add_url_params(endpoint, payload)
data = sess.get(url)
print(data.text)
def get_stream():
stream_name = raw_input("Masukkan nama stream: ").strip()
endpoint = base_url + "/stream"
payload = {
"stream": stream_name
}
url = add_url_params(endpoint, payload)
data = sess.get(url)
print(data.text)
if __name__ == '__main__':
print("\nDemonstrating get location...")
get_location()
print("\nDemonstrating get create post...")
create_post()
print("\nDemonstrating get stream...")
get_stream()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment