-
-
Save thor27/9aee0fb52954c5c3314f 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
# -*- encoding: utf-8 -*- | |
#!/usr/bin/python | |
import httplib, urllib, json, sys | |
from pprint import pprint | |
from datetime import datetime | |
import optparse | |
#URI = 'localhost:5008/static/feed.html' | |
URI = 'g1.globo.com/bemestar/no-ar/index.html' | |
#FEED_HOST = "localhost:5008" | |
FEED_HOST = 'api.feed.qa02.globoi.com' | |
PRINT_REQUEST = False | |
PRINT_RESPONSE = False | |
FAKE_REQUEST = False | |
def promote_post(user_id, post_id): | |
args = {} | |
if user_id: | |
args['GLBID'] = str(user_id) | |
path = "/feeds/%s/posts/%s/publish" % (URI, post_id) | |
params = urllib.urlencode(args) | |
if PRINT_REQUEST: | |
print 'curl -X PUT -d "%s" http://%s%s' % (urllib.unquote_plus(params), FEED_HOST, path) | |
if FAKE_REQUEST: | |
return 0 | |
conn = httplib.HTTPConnection(FEED_HOST) | |
conn.request("PUT", path, params) | |
response = conn.getresponse() | |
if response.status == 200: | |
data = json.loads(response.read()) | |
if PRINT_RESPONSE: | |
pprint(data) | |
return response.status | |
if PRINT_RESPONSE: | |
pprint({'Error': (response.status, response.reason)}) | |
return response.status | |
def get_posts(user_id, parent_id=None, post_id=None): | |
args = {'uri': URI} | |
if user_id: | |
args['GLBID'] = str(user_id) | |
if parent_id: | |
args['parent_id'] = parent_id | |
params = urllib.urlencode(args) | |
if post_id: | |
path = "/postdetail/%s/posts/%s" % (URI, post_id) | |
else: | |
path = "/post/%s" % URI | |
if PRINT_REQUEST: | |
print 'curl -X GET -d "%s" http://%s%s' % (urllib.unquote_plus(params), FEED_HOST, path) | |
if FAKE_REQUEST: | |
return 0 | |
conn = httplib.HTTPConnection(FEED_HOST) | |
conn.request("GET", path, params) | |
response = conn.getresponse() | |
if response.status == 200: | |
data = json.loads(response.read()) | |
if PRINT_RESPONSE: | |
pprint(data) | |
return response.status | |
if PRINT_RESPONSE: | |
pprint({'Error': (response.status, response.reason)}) | |
return response.status | |
def add_post(user_id, parent_id=None, body=None): | |
if not body: | |
body = "Sample text for user %d at %s" %(user_id, datetime.now().isoformat()) | |
args = {'uri': URI, 'body': body} | |
if user_id: | |
args['GLBID'] = str(user_id) | |
if parent_id: | |
args['parent_id'] = parent_id | |
params = urllib.urlencode(args) | |
path = "/post/%s" %URI | |
if PRINT_REQUEST: | |
print 'curl -X POST -d "%s" http://%s%s' %( urllib.unquote_plus(params), FEED_HOST, path) | |
if FAKE_REQUEST: | |
return 0 | |
conn = httplib.HTTPConnection(FEED_HOST) | |
conn.request("POST", path, params) | |
response = conn.getresponse() | |
if response.status == 200: | |
data = json.loads(response.read()) | |
if PRINT_RESPONSE: | |
pprint(data) | |
return response.status | |
if PRINT_RESPONSE: | |
pprint({'Error': (response.status, response.reason)}) | |
return response.status | |
if __name__ == "__main__": | |
parser = optparse.OptionParser() | |
parser.add_option('-t', '--type', action="store", dest="type", choices=['add','promote','get'], help="Set type of request", default='add') | |
parser.add_option('-u', '--user_id', action="store", dest="user_id", type="int", help="User ID", default=1001) | |
parser.add_option('-p', '--post_id', action="store", dest="post_id", help="Post/Reply ID", default=None) | |
parser.add_option('-a', '--parent_id', action="store", dest="parent_id", help="Parent post ID", default=None) | |
parser.add_option('-d', '--body', action="store", dest="body", help="Post body text", default=None) | |
parser.add_option('-n', '--hostname', action="store", dest="host", help="Set hostname", default=FEED_HOST) | |
parser.add_option('-c', '--uri', action="store", dest="uri", help="Set uri", default=URI) | |
parser.add_option('-b', '--batch-size', action="store", dest="batch", type="int", help="How many posts to create", default=1) | |
parser.add_option('-r', '--print_request', action="store_true", dest="print_request", help="Post/Parent post ID", default=PRINT_REQUEST) | |
parser.add_option('-o', '--print_response', action="store_true", dest="print_response", help="Post/Parent post ID", default=PRINT_RESPONSE) | |
parser.add_option('-f', '--fake_request', action="store_true", dest="fake_request", help="Post/Parent post ID", default=FAKE_REQUEST) | |
(options, args) = parser.parse_args() | |
URI = options.uri | |
FEED_HOST = options.host | |
PRINT_REQUEST = options.print_request | |
PRINT_RESPONSE = options.print_response | |
FAKE_REQUEST = options.fake_request | |
for x in range(options.batch): | |
if options.type == 'add': | |
add_post(options.user_id, options.parent_id, options.body) | |
elif options.type == 'promote' and options.post_id: | |
promote_post(options.user_id, options.post_id) | |
elif options.type == 'get': | |
get_posts(options.user_id, options.parent_id, options.post_id) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment