Created
December 27, 2020 08:37
-
-
Save ulgens/b7b6cd25459caa0625e7c134f25bbf0f to your computer and use it in GitHub Desktop.
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
# 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