Created
May 15, 2019 16:14
-
-
Save iandanforth/390ddab0c29ee79a26aadb5ab20b86ef to your computer and use it in GitHub Desktop.
Remove all files from plotly account
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
import plotly | |
import plotly.plotly as py | |
import json | |
import requests | |
from requests.auth import HTTPBasicAuth | |
username = '' # Replace with YOUR USERNAME | |
api_key = '' # Replace with YOUR API KEY | |
auth = HTTPBasicAuth(username, api_key) | |
headers = {'Plotly-Client-Platform': 'python'} | |
def get_pages(username, page_size): | |
url = 'https://api.plot.ly/v2/folders/all?user='+username+'&page_size='+str(page_size) | |
response = requests.get(url, auth=auth, headers=headers) | |
if response.status_code != 200: | |
return | |
page = json.loads(response.content) | |
yield page | |
while True: | |
resource = page['children']['next'] | |
if not resource: | |
break | |
response = requests.get(resource, auth=auth, headers=headers) | |
if response.status_code != 200: | |
break | |
page = json.loads(response.content) | |
yield page | |
def permanently_delete_files(username, page_size=500, filetype_to_delete='plot'): | |
count = 0 | |
for page in get_pages(username, page_size): | |
for x in range(0, len(page['children']['results'])): | |
fid = page['children']['results'][x]['fid'] | |
res = requests.get('https://api.plot.ly/v2/files/' + fid, auth=auth, headers=headers) | |
res.raise_for_status() | |
if res.status_code == 200: | |
json_res = json.loads(res.content) | |
if json_res['filetype'] == filetype_to_delete: | |
# move to trash | |
requests.post('https://api.plot.ly/v2/files/'+fid+'/trash', auth=auth, headers=headers) | |
# permanently delete | |
count += 1 | |
print(count) | |
requests.delete('https://api.plot.ly/v2/files/'+fid+'/permanent_delete', auth=auth, headers=headers) | |
permanently_delete_files(username, filetype_to_delete='plot') | |
permanently_delete_files(username, filetype_to_delete='grid') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment