Last active
August 29, 2015 14:18
-
-
Save Resisty/8f2bfce005ba209733f5 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
def getFromSite(self, search, contentonly = False, thingtype = 'contents'): | |
thingtypes = ['contents', 'places', 'people'] | |
if thingtype not in thingtypes: | |
thingtype = 'contents' | |
query = 'https://example.com/api/core/v3/search/{0}?filter=search("{1}")' | |
setquery = query.format(thingtype, search) | |
returnedpage = requests.get(setquery, | |
auth = (self.settings['user'], self.settings['pass']), | |
headers = {'content-type': 'application/json'}) | |
jsondata = ''.join(returnedpage.text.split('\n')[1:]) | |
data = json.loads(jsondata) | |
for i in data['list']: | |
if thingtype == 'contents' and i['subject'] == search: | |
if contentonly: | |
return i['content'] | |
else: | |
return i | |
elif thingtype == 'places' and i['name'] == search: | |
return i | |
return data | |
def putToSite(self, subject, html): | |
try: | |
contentID = self.getFromSite(subject)['contentID'] | |
except KeyError: | |
return {'error': 'Could not find contentID for search {0}. Please try again.'.format(subject)} | |
reqdict = { 'url': 'https://example.com/api/core/v3/contents/{0}'.format(contentID), | |
'params': { 'type' : 'document', | |
'status' : 'published', | |
'subject' : subject, | |
'content' : { 'type' : 'text/html', | |
'text' : html | |
} | |
} | |
} | |
todaysDate = time.strftime("%F-%T") | |
params = json.dumps(reqdict['params']) | |
returnedPage = requests.put(reqdict['url'], | |
data=params, | |
auth=(self.settings['user'], self.settings['pass']), | |
headers={'content-type': 'application/json'}) | |
okcodes = [200, 201] | |
if any([i == returnedPage.status_code for i in okcodes]): | |
print 'Successfully created doc' | |
jsonData = json.loads(returnedPage.text) | |
return jsonData['resources']['html']['ref'] | |
else: | |
print 'Failed creating doc.:' | |
print returnedPage.status_code | |
#print returnedPage.text | |
return returnedPage.text |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment