Browse Source

Source Code

main
Fsoky 4 years ago
parent
commit
427c180cc4
  1. 65
      donationalerts_api.py

65
donationalerts_api.py

@ -6,18 +6,29 @@ from websocket import create_connection
class Scopes: class Scopes:
user_show = "oauth-user-show" USER_SHOW = "oauth-user-show"
donation_subscribe = "oauth-donation-subscribe" DONATION_SUBSCRIBE = "oauth-donation-subscribe"
donation_index = "oauth-donation-index" DONATION_INDEX = "oauth-donation-index"
custom_alert_store = "oauth-custom_alert-store" CUSTOM_ALERT_STORE = "oauth-custom_alert-store"
goal_subscribe = "oauth-goal-subscribe" GOAL_SUBSCRIBE = "oauth-goal-subscribe"
poll_subscribe = "oauth-poll-subscribe" POLL_SUBSCRIBE = "oauth-poll-subscribe"
all_scopes = [user_show, donation_subscribe, donation_index, custom_alert_store, ALL_SCOPES = [USER_SHOW, DONATION_INDEX, DONATION_SUBSCRIBE, CUSTOM_ALERT_STORE,
goal_subscribe, poll_subscribe] GOAL_SUBSCRIBE, POLL_SUBSCRIBE]
class Channels:
NEW_DONATION_ALERTS = "$alerts:donation_"
DONATION_GOALS_UPDATES = "$goals:goal_"
POLLS_UPDATES = "$polls:poll_"
ALL_CHANNELS = [NEW_DONATION_ALERTS, DONATION_GOALS_UPDATES, POLLS_UPDATES]
class DonationAlertsApi: class DonationAlertsApi:
@ -52,15 +63,12 @@ class DonationAlertsApi:
self.donations_api = "https://www.donationalerts.com/api/v1/alerts/donations" self.donations_api = "https://www.donationalerts.com/api/v1/alerts/donations"
self.custom_alerts_api = "https://www.donationalerts.com/api/v1/custom_alert" self.custom_alerts_api = "https://www.donationalerts.com/api/v1/custom_alert"
def login(self): def login(self):
return self.login_url return self.login_url
def get_code(self): def get_code(self):
return request.args.get("code") return request.args.get("code")
def get_access_token(self, code): def get_access_token(self, code):
payload = { payload = {
"client_id": self.client_id, "client_id": self.client_id,
@ -74,7 +82,6 @@ class DonationAlertsApi:
access_token = requests.post(url=self.token_url, data=payload).json() access_token = requests.post(url=self.token_url, data=payload).json()
return access_token.get("access_token") return access_token.get("access_token")
def get_donations(self, access_token): def get_donations(self, access_token):
headers = { headers = {
"Authorization": f"Bearer {access_token}", "Authorization": f"Bearer {access_token}",
@ -82,8 +89,7 @@ class DonationAlertsApi:
} }
donate_object = requests.get(url=self.donations_api, headers=headers).json() donate_object = requests.get(url=self.donations_api, headers=headers).json()
return donate_object # HERE PROBLEMS!!! return donate_object
def get_user(self, access_token): def get_user(self, access_token):
headers = { headers = {
@ -94,7 +100,6 @@ class DonationAlertsApi:
return user_object return user_object
def send_custom_alert(self, access_token, external_id, headline, message, image_url=None, sound_url=None, is_shown=0): def send_custom_alert(self, access_token, external_id, headline, message, image_url=None, sound_url=None, is_shown=0):
headers = { headers = {
"Authorization": f"Bearer {access_token}", "Authorization": f"Bearer {access_token}",
@ -136,27 +141,31 @@ class Centrifugo:
return self.ws_response return self.ws_response
def subscribe(self): def subscribe(self, channels):
chnls = []
for channel in channels:
chnls.append(f"{channel}{self.user_id}")
headers = { headers = {
"Authorization": f"Bearer {self.access_token}", "Authorization": f"Bearer {self.access_token}",
"Content-Type": "application/json" "Content-Type": "application/json"
} }
data = { data = {
"channels": [f"$alerts:donation_{self.user_id}"], "channels": chnls,
"client": self.ws_response["result"]["client"] "client": self.ws_response["result"]["client"]
} }
response = requests.post(url="https://www.donationalerts.com/api/v1/centrifuge/subscribe", data=json.dumps(data), headers=headers).json() response = requests.post(url="https://www.donationalerts.com/api/v1/centrifuge/subscribe", data=json.dumps(data), headers=headers).json()
for ch in response["channels"]:
self.ws.send(json.dumps( self.ws.send(json.dumps(
{ {
"params": { "params": {
"channel": f"$alerts:donation_{self.user_id}", "channel": ch["channel"],
"token": response["channels"][0]["token"] "token": ch["token"]
}, },
"method": 1, "method": 1,
"id": self.user_id "id": self.user_id
} }
)) ))
return json.loads(self.ws.recv()) return json.loads(self.ws.recv())
Loading…
Cancel
Save