Skip to content

Instantly share code, notes, and snippets.

@iandanforth
Created May 15, 2019 16:14
Show Gist options
  • Select an option

  • Save iandanforth/390ddab0c29ee79a26aadb5ab20b86ef to your computer and use it in GitHub Desktop.

Select an option

Save iandanforth/390ddab0c29ee79a26aadb5ab20b86ef to your computer and use it in GitHub Desktop.

Revisions

  1. iandanforth created this gist May 15, 2019.
    50 changes: 50 additions & 0 deletions clean_plotly.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,50 @@
    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')