libere-tes-chaine-de-mots/import_data/32_importation_linkedin_comments.py
2024-10-02 21:53:37 -04:00

58 lines
1.7 KiB
Python

import pandas as pd
import datetime
import os
from pathlib import Path
from utils.documents_to_database import documents_to_database
#%% 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
linkedin_data_path = os.path.join(project_root, 'import_data', 'data', 'LinkedIn', 'comments', 'Comments.csv')
#%% In[ ]:
raw_comments_csv = pd.read_csv(linkedin_data_path,
escapechar='\\',
skipinitialspace=True)
raw_comments_csv['MessageFix'] = raw_comments_csv['Message'].str.replace(r'[\r\n\t]+', ' ', regex=True)
raw_comments_csv = raw_comments_csv.drop(columns=['Message'])
raw_comments = raw_comments_csv[(raw_comments_csv['MessageFix'] != "")].drop_duplicates()
#%% In[ ]:
raw_comments['index'] = "rs_linkedin_comments"
raw_comments['type'] = "comments"
raw_comments['network'] = "LinkedIn"
raw_comments['chemin'] = linkedin_data_path
#%% In[ ]:
raw_comments["creation_timestamp"] = raw_comments["Date"].apply(
lambda x: int(datetime.datetime.fromisoformat(x).timestamp())
)
del raw_comments["Date"]
#%% In[ ]:
raw_comments.rename(columns={"Link": "uri", "MessageFix": "texte"}, inplace=True)
#%% In[ ]:
raw_comments["chemin"] = linkedin_data_path
#%% In[ ]:
raw_comments.fillna(value="", inplace=True)
#%% In[ ]:
raw_comments.drop_duplicates(subset=['texte', 'creation_timestamp'], inplace=True)
#%% In[ ]:
# Filter empty texte
raw_comments = raw_comments[~raw_comments['texte'].str.strip('\n').str.strip().eq('')]
#%% In[ ]:
documents_to_database(raw_comments)