Created
August 29, 2014 05:17
-
-
Save dpawluk/0aefce25b7237c23a684 to your computer and use it in GitHub Desktop.
Stupid random utility I wrote to play with paging requests in pythons. Gets all and deletes HC articles...still not up to par for pagination. Flattening needs better list comprehension logic.
This file contains hidden or 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
| import requests | |
| from requests.auth import HTTPBasicAuth | |
| import json | |
| auth = HTTPBasicAuth('EMAIL_ADDR/token', 'TOKEN') | |
| headers = {'content-type': 'application/json'} | |
| s = requests.Session() | |
| def get_all_articles(r_array, next): | |
| if(next == None): | |
| url = "https://SUBDOMAIN.zendesk.com/api/v2/help_center/articles.json" | |
| else: | |
| url = next | |
| response = s.get(url, auth=auth) | |
| jresponse = response.json() | |
| if(jresponse['next_page'] == None): | |
| r_array.append(jresponse['articles']) | |
| flat_array = [y for x in r_array for y in x] | |
| len(flat_array) | |
| return flat_array | |
| else: | |
| r_array.append(jresponse['articles']) | |
| return get_all_articles(r_array, jresponse['next_page']) | |
| def delete_articles(ids): | |
| count = len(ids) | |
| for y in range(0, count): | |
| url = "https://SUBDOMAIN.zendesk.com/api/v2/help_center/articles/{0}.json".format(ids[y]) | |
| print(url) | |
| response = s.delete(url, headers=headers, auth=auth) | |
| print("{0} ********************************************************* STATUS: {1}\n".format(y, response.status_code)) | |
| print(str(response.text)) | |
| print("\n") | |
| print("--------*******************************************--------") | |
| return | |
| articles = get_all_articles([], next=None) | |
| count = len(articles) | |
| print("Deleting a total of {0} articles now".format(count)) | |
| ids = [] | |
| for x in range(0, count): | |
| ids.append(articles[x]['id']) | |
| delete_articles(ids) | |
| print(ids) | |
| print("ALL DONE") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment