Created
May 16, 2019 01:34
-
-
Save kalsky/9008cb54c844cd25df983ebb8a8736f6 to your computer and use it in GitHub Desktop.
This file helps you download the ideas of your AnswerHub community side
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
#Author: Yaniv Kalsky | |
import requests | |
import csv | |
import os | |
HOST = 'http://yourcompany.cloud.answerhub.com' | |
AUTH = "Basic bWFu2XYxa4p5MTlvMTV4" | |
community_base_url = 'https://community.yourcompany.com' | |
MAX_IDEAS = 1000 | |
def get_from_address(address, lists=False): | |
""" | |
Simple GET from address, with Boolean options to narrow down return | |
:param address: String, url to GET from | |
:param lists: Boolean, specify to get list list from dictionary returned | |
:return: json response or portion of response | |
""" | |
headers = { | |
'accept': "application/json", | |
'content-type': "application/json", | |
'Authorization': AUTH | |
} | |
try: | |
response = requests.request("GET", address, headers=headers) | |
if response.ok == True: | |
if lists: | |
return response.json()['list'] | |
return response.json() | |
else: | |
return {} | |
except StandardError as err: | |
print err.message | |
return {} | |
def _to_utf8(lst): | |
return [unicode(elem).encode('utf-8') for elem in lst] | |
def _write_to_csv(destination, lines=None): | |
if lines is None: | |
lines = [] | |
try: | |
with open(destination, 'wb') as f: | |
csvout = csv.writer(f) | |
for line in lines: | |
csvout.writerow(_to_utf8(line)) | |
csvout.writerow([' ']) | |
f.close() | |
except StandardError as err: | |
print (err.message) | |
def build_csv(ideas, csv_path): | |
""" | |
:param list[json] ideas: | |
:return: | |
""" | |
lines = [['id', 'Title', 'Body', 'Creation Date', 'Username', 'Full Name', 'Votes', 'State', 'URL']] | |
cur = 1 | |
total = str(min(len(ideas), MAX_IDEAS)) | |
for idea in ideas: | |
print "getting idea #{} ({}/{})".format(idea["id"], str(cur), total) | |
idea_details = get_from_address(HOST + '/services/v2/idea/{}.json'.format(idea["id"])) | |
line = [idea_details["id"], | |
idea_details["title"], | |
idea_details["body"], | |
idea_details["creationDateFormatted"], | |
idea_details["author"]["username"], | |
idea_details["author"].get("realname", ""), | |
idea_details["upVoteCount"], | |
idea_details["nodeStates"][0].replace('idea-','').replace('_',' '), | |
'{}/idea/{}/index.html'.format(community_base_url, idea_details["id"])] | |
lines.append(line) | |
cur += 1 | |
if cur>MAX_IDEAS: | |
break | |
_write_to_csv(csv_path, lines) | |
############################################################################### | |
def main(): | |
try: | |
print "Getting the list of ideas..." | |
ideas = get_from_address(HOST + '/services/v2/idea.json?sort=newest&pageSize='.format(MAX_IDEAS), lists=True) | |
print "Got {} ideas".format(str(len(ideas))) | |
print "Now getting ideas details to build the csv..." | |
csv_path = '{}/ideas.csv'.format(os.getcwd()) | |
build_csv(ideas, csv_path) | |
print "The csv is available here: " + csv_path | |
except Exception as e: | |
print e.message | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment