Spaces:
Sleeping
Sleeping
joshuadunlop
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import requests
|
4 |
+
|
5 |
+
# Function to create a tag in Maropost
|
6 |
+
def create_tag(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/tags.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 tags
|
20 |
+
def create_tags_from_csv(csv_file, auth_token):
|
21 |
+
df = pd.read_csv(csv_file)
|
22 |
+
success_tags = []
|
23 |
+
failed_tags = []
|
24 |
+
|
25 |
+
for _, row in df.iterrows():
|
26 |
+
data = {
|
27 |
+
"tag": {
|
28 |
+
"name": row['name'], # Assuming 'name' column contains the tag name
|
29 |
+
}
|
30 |
+
}
|
31 |
+
response = create_tag(data, auth_token)
|
32 |
+
if response.status_code in [200, 201]:
|
33 |
+
success_tags.append(row['name'])
|
34 |
+
else:
|
35 |
+
failed_tags.append((row['name'], response.status_code, response.text))
|
36 |
+
|
37 |
+
return success_tags, failed_tags
|
38 |
+
|
39 |
+
# Streamlit UI
|
40 |
+
def main():
|
41 |
+
st.title("Maropost Tags Creator")
|
42 |
+
|
43 |
+
auth_token = st.text_input("Enter your Maropost API auth token:", type="password")
|
44 |
+
csv_file = st.file_uploader("Upload CSV file for tags", type=['csv'])
|
45 |
+
|
46 |
+
if st.button("Create Tags"):
|
47 |
+
if not auth_token or csv_file is None:
|
48 |
+
st.error("Please enter your auth token and upload a CSV file.")
|
49 |
+
else:
|
50 |
+
success_tags, failed_tags = create_tags_from_csv(csv_file, auth_token)
|
51 |
+
if success_tags:
|
52 |
+
st.success(f"Successfully created tags: {', '.join(success_tags)}.")
|
53 |
+
if failed_tags:
|
54 |
+
for failed_tag, status_code, message in failed_tags:
|
55 |
+
st.error(f"Failed to create tag {failed_tag}. Response code: {status_code}, Message: {message}")
|
56 |
+
|
57 |
+
if __name__ == "__main__":
|
58 |
+
main()
|