Skip to content

Instantly share code, notes, and snippets.

@DeusFigendi
Last active November 25, 2023 07:45
Show Gist options
  • Save DeusFigendi/aa758dea43fe6aeb1538c7c3d336ee30 to your computer and use it in GitHub Desktop.
Save DeusFigendi/aa758dea43fe6aeb1538c7c3d336ee30 to your computer and use it in GitHub Desktop.
analyses a diaspora profile export to find the oldest posting
#!/usr/bin/python3
import json
from datetime import datetime
from os import listdir
import sys
def get_pod_base_url_from_handle(handle):
return(handle.split('@')[-1])
def normalize_isodatetime(dt):
if (dt[-1] == 'Z'):
return(datetime.fromisoformat(dt[0:-1]))
else:
return(datetime.fromisoformat(dt))
list_of_files = listdir('./')
list_of_json_files = []
for filename in list_of_files:
if (filename[-5:].lower() == '.json'):
list_of_json_files.append(filename)
if (len(list_of_json_files) == 0):
print('no json-file found… exiting')
sys.exit()
elif (len(list_of_json_files) == 1):
pro_file_name = list_of_json_files[0]
else:
print('which file do you want to analyse?')
for i in range(0,len(list_of_json_files)):
print('['+str(i)+'] '+list_of_json_files[i])
chosen_filenumber = -1
while (chosen_filenumber < 0):
chosen_filenumberstring = input('choose a file (0-'+str(len(list_of_json_files)-1)+') ')
try:
chosen_filenumber = int(chosen_filenumberstring)
except:
chosen_filenumber = -1
if (chosen_filenumber > len(list_of_json_files)-1):
chosen_filenumber = -1
pro_file_name = list_of_json_files[chosen_filenumber]
with open(pro_file_name) as pro_file:
all_the_data = json.load(pro_file)
valid_profile = False
if ('user' in all_the_data):
if ('posts' in all_the_data['user']):
if ('entity_data' in all_the_data['user']['posts'][0]):
if ('guid' in all_the_data['user']['posts'][0]['entity_data']):
valid_profile = True
if (valid_profile == False):
print('json-file is no valid profile… exiting')
sys.exit()
pod_baseurl = get_pod_base_url_from_handle(all_the_data['user']['profile']['entity_data']['author'])
oldest_postings_date = datetime.now()
for this_post in all_the_data['user']['posts']:
if (this_post['entity_type'] == 'status_message'):
this_postings_date = normalize_isodatetime(this_post['entity_data']['created_at'])
if (this_postings_date < oldest_postings_date):
oldest_postings_date = this_postings_date
print(this_post['entity_data']['created_at']+' - https://'+pod_baseurl+'/posts/'+this_post['entity_data']['guid'])
if (this_post['entity_type'] == 'status_message'):
this_postings_date = normalize_isodatetime(this_post['entity_data']['created_at'])
if (this_postings_date < oldest_postings_date):
oldest_postings_date = this_postings_date
print(this_post['entity_data']['created_at']+' - https://'+pod_baseurl+'/posts/'+this_post['entity_data']['guid']+' ('+this_post['entity_type']+')')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment