Spaces:
Sleeping
Sleeping
Commit
·
34291e2
1
Parent(s):
5009788
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from urllib import parse, request
|
2 |
+
import hashlib
|
3 |
+
from time import strftime, gmtime
|
4 |
+
import streamlit as st
|
5 |
+
from datetime import datetime, timedelta
|
6 |
+
|
7 |
+
# Function to fetch data from ShareASale
|
8 |
+
def fetch_shareasale_data(action_verb, affiliate_id, api_token, api_secret_key, api_version=2.4):
|
9 |
+
my_timestamp = strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())
|
10 |
+
data = parse.urlencode({
|
11 |
+
'affiliateId': affiliate_id,
|
12 |
+
'token': api_token,
|
13 |
+
'version': api_version,
|
14 |
+
'action': action_verb
|
15 |
+
})
|
16 |
+
|
17 |
+
sig = f"{api_token}:{my_timestamp}:{action_verb}:{api_secret_key}"
|
18 |
+
sig_hash = hashlib.sha256(sig.encode('utf-8')).hexdigest()
|
19 |
+
|
20 |
+
my_headers = {
|
21 |
+
'x-ShareASale-Date': my_timestamp,
|
22 |
+
'x-ShareASale-Authentication': sig_hash
|
23 |
+
}
|
24 |
+
|
25 |
+
call = request.Request(f'https://shareasale.com/x.cfm?{data}', headers=my_headers)
|
26 |
+
|
27 |
+
try:
|
28 |
+
return_result = request.urlopen(call).read()
|
29 |
+
return return_result
|
30 |
+
except Exception as inst:
|
31 |
+
return str(inst)
|
32 |
+
|
33 |
+
# Streamlit UI
|
34 |
+
st.title("Affiliate Earnings Dashboard")
|
35 |
+
st.sidebar.title("Settings")
|
36 |
+
|
37 |
+
# Date range picker
|
38 |
+
start_date = st.sidebar.date_input("Start Date", datetime.now() - timedelta(days=30))
|
39 |
+
end_date = st.sidebar.date_input("End Date", datetime.now())
|
40 |
+
|
41 |
+
# Fetch data button
|
42 |
+
if st.sidebar.button("Fetch Data"):
|
43 |
+
# Placeholder for your API credentials
|
44 |
+
my_affiliate_id = 'YOUR_AFFILIATE_ID'
|
45 |
+
api_token = 'YOUR_API_TOKEN'
|
46 |
+
api_secret_key = 'YOUR_API_SECRET_KEY'
|
47 |
+
|
48 |
+
# Fetch data from ShareASale
|
49 |
+
action_verb = 'ledger'
|
50 |
+
ledger_data = fetch_shareasale_data(action_verb, my_affiliate_id, api_token, api_secret_key)
|
51 |
+
|
52 |
+
# Display data (this is a placeholder, replace with actual parsing and display logic)
|
53 |
+
st.write(ledger_data)
|