import datetime

import pandas as pd
import json

from utils.get_ids import get_idtypedocument, get_idreseausocial
from utils.documents_to_database import documents_to_database
from utils.convert_encoding_meta import convert_encoding_meta

# In[ ]:
fb_data_path = ['data/Facebook/posts/your_uncategorized_photos.json']
with open(fb_data_path[0], "r", encoding="raw-unicode-escape") as posts:
    photos_json = json.loads(convert_encoding_meta(posts.read()))

# In[ ]:
facebook_photos = photos_json['other_photos_v2']

# In[ ]:
facebook_photos_df = pd.DataFrame(facebook_photos)

# In[ ]:
# Filter out posts without a description
facebook_photos_df = facebook_photos_df[~facebook_photos_df['description'].isnull()]

# In[ ]:
facebook_photos_df['datepublication'] = facebook_photos_df['creation_timestamp'].apply(
    lambda x: datetime.datetime.fromtimestamp(x).isoformat())
facebook_photos_df['index'] = "rs_facebook_posts"
facebook_photos_df['network'] = "Facebook"
facebook_photos_df['type'] = "posts"
facebook_photos_df['chemin'] = fb_data_path[0]

# In[ ]:
facebook_photos_df.rename(columns={"description": "texte"}, inplace=True)

# In[ ]:
del facebook_photos_df['creation_timestamp']
del facebook_photos_df['media_metadata']

# In[ ]:
facebook_photos_df.fillna(value="", inplace=True)

# In[ ]:
documents_to_database(facebook_photos_df)
