Spaces:
Runtime error
Runtime error
joshuadunlop
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import requests
|
4 |
+
|
5 |
+
# Function to create a list in Maropost
|
6 |
+
def create_list(data, auth_token):
|
7 |
+
headers = {
|
8 |
+
"Content-Type": "application/json",
|
9 |
+
"Accept": "application/json",
|
10 |
+
}
|
11 |
+
response = requests.post(
|
12 |
+
"https://api.maropost.com/accounts/1783/lists.json",
|
13 |
+
json=data,
|
14 |
+
headers=headers,
|
15 |
+
params={'auth_token': auth_token}
|
16 |
+
)
|
17 |
+
return response
|
18 |
+
|
19 |
+
# Function to read CSV and create lists
|
20 |
+
def create_lists_from_csv(csv_file, auth_token):
|
21 |
+
df = pd.read_csv(csv_file)
|
22 |
+
success_lists = []
|
23 |
+
failed_lists = []
|
24 |
+
|
25 |
+
for _, row in df.iterrows():
|
26 |
+
data = {
|
27 |
+
"list": {
|
28 |
+
"reply_to_email": row['reply-to-email'],
|
29 |
+
"address": row['address'],
|
30 |
+
"from_email": row['from-email'],
|
31 |
+
"language": row['language'],
|
32 |
+
"from_name": row.get('from-name', ""),
|
33 |
+
"name": row['name'],
|
34 |
+
"add_to_unsubscribe_page": row.get('add-to-unsubscribe-page', False),
|
35 |
+
"post_url": None, # Assuming post_url will always be None as per your example
|
36 |
+
"display_name": row.get('display-name', ""),
|
37 |
+
"description": row.get('description', "")
|
38 |
+
}
|
39 |
+
}
|
40 |
+
response = create_list(data, auth_token)
|
41 |
+
if response.status_code in [200, 201]:
|
42 |
+
success_lists.append(row['name'])
|
43 |
+
else:
|
44 |
+
failed_lists.append((row['name'], response.status_code, response.text))
|
45 |
+
|
46 |
+
return success_lists, failed_lists
|
47 |
+
|
48 |
+
# Streamlit UI
|
49 |
+
def main():
|
50 |
+
st.title("Maropost Lists Creator")
|
51 |
+
|
52 |
+
auth_token = st.text_input("Enter your Maropost API auth token:", type="password")
|
53 |
+
csv_file = st.file_uploader("Upload CSV file for lists", type=['csv'])
|
54 |
+
|
55 |
+
if st.button("Create Lists"):
|
56 |
+
if not auth_token or csv_file is None:
|
57 |
+
st.error("Please enter your auth token and upload a CSV file.")
|
58 |
+
else:
|
59 |
+
success_lists, failed_lists = create_lists_from_csv(csv_file, auth_token)
|
60 |
+
if success_lists:
|
61 |
+
st.success(f"Successfully created lists: {', '.join(success_lists)}.")
|
62 |
+
if failed_lists:
|
63 |
+
for failed_list, status_code, message in failed_lists:
|
64 |
+
st.error(f"Failed to create list {failed_list}. Response code: {status_code}, Message: {message}")
|
65 |
+
|
66 |
+
if __name__ == "__main__":
|
67 |
+
main()
|