2024-07-23 04:07:24 +00:00
|
|
|
import pandas as pd
|
|
|
|
import json
|
2024-10-03 01:53:37 +00:00
|
|
|
import os
|
|
|
|
from pathlib import Path
|
2024-07-23 04:07:24 +00:00
|
|
|
|
|
|
|
from utils.documents_to_database import documents_to_database
|
|
|
|
from utils.convert_encoding_meta import convert_encoding_meta
|
|
|
|
|
2024-10-03 01:53:37 +00:00
|
|
|
#%% In[ ]:
|
|
|
|
# Get the current file's directory
|
|
|
|
try:
|
|
|
|
# This will work when running as a script
|
|
|
|
script_dir = Path(__file__).parent.parent
|
|
|
|
except NameError:
|
|
|
|
# This will work in interactive environments
|
|
|
|
script_dir = Path().absolute()
|
|
|
|
|
|
|
|
project_root = script_dir
|
|
|
|
instagram_data_path = os.path.join(project_root, 'import_data', 'data', 'Instagram', 'threads', 'threads_and_replies.json')
|
|
|
|
|
2024-10-04 02:30:39 +00:00
|
|
|
with open(instagram_data_path, "r") as posts:
|
2024-07-23 04:07:24 +00:00
|
|
|
post_comments_1 = json.loads(convert_encoding_meta(posts.read()))
|
|
|
|
|
2024-10-03 01:53:37 +00:00
|
|
|
#%% In[ ]:
|
2024-07-23 04:07:24 +00:00
|
|
|
threads_comments = []
|
|
|
|
for post in post_comments_1['text_post_app_text_posts']:
|
|
|
|
for element in post['media']:
|
|
|
|
threads_comments.append({"texte": element['title'],
|
2024-10-03 01:53:37 +00:00
|
|
|
'creation_timestamp': element['creation_timestamp'],
|
2024-07-23 04:07:24 +00:00
|
|
|
"chemin": instagram_data_path,
|
|
|
|
"index": "rs_instagram_threads",
|
|
|
|
"type": "posts",
|
2024-10-04 02:30:39 +00:00
|
|
|
"network": "Threads"})
|
2024-07-23 04:07:24 +00:00
|
|
|
|
2024-10-03 01:53:37 +00:00
|
|
|
#%% In[ ]:
|
2024-07-23 04:07:24 +00:00
|
|
|
ig_comments_df = pd.DataFrame(threads_comments)
|
|
|
|
|
2024-10-03 01:53:37 +00:00
|
|
|
#%% In[ ]:
|
2024-07-23 04:07:24 +00:00
|
|
|
ig_comments_df.fillna(value="", inplace=True)
|
|
|
|
|
2024-10-03 01:53:37 +00:00
|
|
|
#%% In[ ]:
|
|
|
|
ig_comments_df.drop_duplicates(subset=['texte', 'creation_timestamp'], inplace=True)
|
2024-07-23 04:07:24 +00:00
|
|
|
|
2024-10-03 01:53:37 +00:00
|
|
|
#%% In[ ]:
|
2024-07-23 04:07:24 +00:00
|
|
|
# Filter empty texte
|
|
|
|
ig_comments_df = ig_comments_df[~ig_comments_df['texte'].str.strip('\n').str.strip().eq('')]
|
|
|
|
|
2024-10-03 01:53:37 +00:00
|
|
|
#%% In[ ]:
|
2024-07-23 04:07:24 +00:00
|
|
|
documents_to_database(ig_comments_df)
|