Maropost-Tags / app.py
joshuadunlop's picture
Create app.py
cb59d2e verified
import streamlit as st
import pandas as pd
import requests
# Function to create a tag in Maropost
def create_tag(data, auth_token):
headers = {
"Content-Type": "application/json",
"Accept": "application/json",
}
response = requests.post(
"https://api.maropost.com/accounts/1783/tags.json",
json=data,
headers=headers,
params={'auth_token': auth_token}
)
return response
# Function to read CSV and create tags
def create_tags_from_csv(csv_file, auth_token):
df = pd.read_csv(csv_file)
success_tags = []
failed_tags = []
for _, row in df.iterrows():
data = {
"tag": {
"name": row['name'], # Assuming 'name' column contains the tag name
}
}
response = create_tag(data, auth_token)
if response.status_code in [200, 201]:
success_tags.append(row['name'])
else:
failed_tags.append((row['name'], response.status_code, response.text))
return success_tags, failed_tags
# Streamlit UI
def main():
st.title("Maropost Tags Creator")
auth_token = st.text_input("Enter your Maropost API auth token:", type="password")
csv_file = st.file_uploader("Upload CSV file for tags", type=['csv'])
if st.button("Create Tags"):
if not auth_token or csv_file is None:
st.error("Please enter your auth token and upload a CSV file.")
else:
success_tags, failed_tags = create_tags_from_csv(csv_file, auth_token)
if success_tags:
st.success(f"Successfully created tags: {', '.join(success_tags)}.")
if failed_tags:
for failed_tag, status_code, message in failed_tags:
st.error(f"Failed to create tag {failed_tag}. Response code: {status_code}, Message: {message}")
if __name__ == "__main__":
main()