Skip to content

Instantly share code, notes, and snippets.

@ulgens
Created December 27, 2020 08:37
Show Gist options
  • Save ulgens/b7b6cd25459caa0625e7c134f25bbf0f to your computer and use it in GitHub Desktop.
Save ulgens/b7b6cd25459caa0625e7c134f25bbf0f to your computer and use it in GitHub Desktop.
# pip install "httpx>=0.16.1"
# Make a dismiss request from https://www.kadenze.com/notifications page manually.
# Get x-csrf-token and cookie from that request, fill HEADERS with them.
import httpx
from pprint import pprint
HEADERS = {
"accept": "application/json, text/javascript, */*; q=0.01",
"accept-language": "en-US,en;q=0.9,tr;q=0.8,tr-TR;q=0.7",
"cookie": "OMITTED",
"content-type": "application/json",
"x-csrf-token": "OMITTED",
"x-requested-with": "XMLHttpRequest",
}
def get_new_notification_ids(page_number):
# FIXME: Retry for timeout
response = httpx.get(
"https://www.kadenze.com/notifications",
headers=HEADERS,
params={"page": page_number},
)
notifications = response.json()["notifications"]
return [no["id"] for no in notifications if not no["viewed"]]
def dismiss_notification(notification_id):
# FIXME: Retry for timeout
data = '{"notification": {"dismissed": true}}'
response = httpx.patch(
f"https://www.kadenze.com/notifications/{notification_id}",
headers=HEADERS,
data=data,
)
return response
if __name__ == "__main__":
page_number = 1
notifications_left = True
all_new_notifications = []
while notifications_left:
print(f"Getting notifications: Page {page_number}...")
notification_ids = get_new_notification_ids(page_number)
if notification_ids:
all_new_notifications.extend(notification_ids)
page_number += 1
else:
notifications_left = False
print(f"{len(all_new_notifications)} notifications found.")
for index, notification_id in enumerate(all_new_notifications, 1):
print(f"{index}- Dismissing {notification_id}...")
response = dismiss_notification(notification_id)
if response.status_code == httpx.codes.ACCEPTED:
print(f"Dismissed {notification_id}")
else:
print(response.json())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment