Created
September 24, 2019 09:37
-
-
Save tomasbedrich/8ff1a82a68da6a30ee40b5aa3929443f to your computer and use it in GitHub Desktop.
Download all events from Sentry project to JSON files
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
#!/usr/bin/env python | |
# coding: utf-8 | |
# In[ ]: | |
import requests | |
import json | |
import time | |
# In[ ]: | |
BASE_URL = "sentry.SOME-COMPANY.com" | |
ORGANIZATION = "SOME-COMPANY" | |
PROJECT = "SOME-PROJECT" | |
TOKEN = "SOME-TOKEN" | |
# In[ ]: | |
def has_next_page(res): | |
pr, nx = res.headers["Link"].split(",") | |
return "true" in nx.split(";")[2] | |
# In[ ]: | |
def get_next_link(res): | |
pr, nx = res.headers["Link"].split(",") | |
return nx.split(";")[0].strip("<> ") | |
# In[ ]: | |
def write_to_file(res, filename): | |
with open(filename, mode="w", encoding="utf-8") as f: | |
json.dump(res.json(), f, indent=4) | |
# In[ ]: | |
url = f"https://{BASE_URL}/api/0/projects/{ORGANIZATION}/{PROJECT}/events/" | |
headers = {"Authorization": f"Bearer {TOKEN}"} | |
i = 0 | |
while i == 0 or has_next_page(res): | |
print(f"page {i} ...", end="") | |
res = requests.get(url, headers=headers) | |
res.raise_for_status() | |
write_to_file(res, f"{PROJECT}-{i:04}.json") | |
i += 1 | |
url = get_next_link(res) | |
print(" [v]") | |
time.sleep(2) | |
print("done") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment