Spaces:
Runtime error
Runtime error
add line_function.py
Browse files- line_fucntion.py +106 -0
- requirements.txt +2 -0
line_fucntion.py
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
#DEF ZONE
|
| 3 |
+
def line_header(CHANNEL_LINE):
|
| 4 |
+
LINE_HEADER = {"Content-Type": "application/json", "Authorization": f"Bearer {CHANNEL_LINE}"}
|
| 5 |
+
return LINE_HEADER
|
| 6 |
+
|
| 7 |
+
def add_new_line_audience(file_path, audience_name, id_column='line_uid', description_key='description', is_ifa_audience="false"):
|
| 8 |
+
'''
|
| 9 |
+
Generates a payload for creating a new LINE audience based on a CSV file containing user IDs.
|
| 10 |
+
Args:
|
| 11 |
+
file_path (str): The path to the CSV file containing the audience data.
|
| 12 |
+
audience_name (str): The name or description of the audience, which will be included in the payload.
|
| 13 |
+
id_column (str, optional): The column name in the CSV file that contains the unique audience IDs. Default is 'line_uid'.
|
| 14 |
+
description_key (str, optional): The key name used in the payload for the audience description. Default is 'description'.
|
| 15 |
+
is_ifa_audience (str, optional): A flag indicating whether the audience is an IFA audience. Default is "false".
|
| 16 |
+
Returns:
|
| 17 |
+
dict: A dictionary payload with the following structure:
|
| 18 |
+
{
|
| 19 |
+
"description_key": "audience_name",
|
| 20 |
+
"isIfaAudience": "is_ifa_audience",
|
| 21 |
+
"audiences": [{"id": audience_id_1}, {"id": audience_id_2}, ...]
|
| 22 |
+
}
|
| 23 |
+
The 'audiences' list will contain the unique audience IDs found in the specified `id_column` of the CSV.
|
| 24 |
+
'''
|
| 25 |
+
|
| 26 |
+
df = pd.read_csv(file_path)
|
| 27 |
+
payload = {
|
| 28 |
+
description_key: audience_name,
|
| 29 |
+
"isIfaAudience": is_ifa_audience,
|
| 30 |
+
"audiences": []
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
# Loop through the unique audience IDs and add them to the payload
|
| 34 |
+
for audience_id in df[id_column].unique():
|
| 35 |
+
payload["audiences"].append({"id": audience_id})
|
| 36 |
+
|
| 37 |
+
return payload
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def update_line_audience(file_path, audience_id):
|
| 41 |
+
'''
|
| 42 |
+
Generates a payload to update an existing LINE audience group with user IDs from a CSV file.
|
| 43 |
+
Args:
|
| 44 |
+
file_path (str): The path to the CSV file containing the audience data.
|
| 45 |
+
audience_id (int or str): The ID of the existing audience group that needs to be updated.
|
| 46 |
+
Returns:
|
| 47 |
+
dict: A dictionary payload with the following structure:
|
| 48 |
+
{
|
| 49 |
+
"audienceGroupId": audience_id,
|
| 50 |
+
"audiences": [{"id": audience_id_1}, {"id": audience_id_2}, ...]
|
| 51 |
+
}
|
| 52 |
+
The 'audiences' list will contain the unique audience IDs found in the 'line_uid' column of the CSV.
|
| 53 |
+
'''
|
| 54 |
+
|
| 55 |
+
# Load the CSV into a DataFrame
|
| 56 |
+
df = pd.read_csv(file_path)
|
| 57 |
+
|
| 58 |
+
# Prepare the payload with the audience group ID and empty audiences list
|
| 59 |
+
payload = {
|
| 60 |
+
"audienceGroupId": int(audience_id),
|
| 61 |
+
"audiences": []
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
# Loop through the unique audience IDs and add them to the payload
|
| 65 |
+
for audience_id in df['line_uid'].unique():
|
| 66 |
+
payload["audiences"].append({"id": audience_id})
|
| 67 |
+
|
| 68 |
+
return payload
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
def single_message(message):
|
| 72 |
+
json = {
|
| 73 |
+
"type": "text",
|
| 74 |
+
"text": f"{message}",
|
| 75 |
+
}
|
| 76 |
+
return json
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
def message_push(to, messageObject):
|
| 80 |
+
json = {
|
| 81 |
+
"to": to,
|
| 82 |
+
"messages":messageObject
|
| 83 |
+
}
|
| 84 |
+
return json
|
| 85 |
+
|
| 86 |
+
def image_map_single(altText, imageLink, ladingpage, width, height):
|
| 87 |
+
data = {
|
| 88 |
+
"type": "imagemap",
|
| 89 |
+
"baseUrl": imageLink,
|
| 90 |
+
"altText": altText,
|
| 91 |
+
"baseSize": {
|
| 92 |
+
"width": int(width),
|
| 93 |
+
"height": int(height)
|
| 94 |
+
}, "actions": [
|
| 95 |
+
{
|
| 96 |
+
"type": "uri",
|
| 97 |
+
"linkUri": ladingpage,
|
| 98 |
+
"area": {
|
| 99 |
+
"x": 0,
|
| 100 |
+
"y": 0,
|
| 101 |
+
"width": int(width),
|
| 102 |
+
"height": int(height)
|
| 103 |
+
}
|
| 104 |
+
}]
|
| 105 |
+
}
|
| 106 |
+
return data
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
pandas==2.2.
|
| 2 |
+
gradio==4.44.1
|