archive2txt/account2txt.py

142 lines
5.5 KiB
Python
Executable File

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Friendica account2txt
This script will take an exported account file from Friendica and
convert it into a couple of text files that contain the information.
Usage:
account2txt [-h] [-i input-file]
-h displays brief help
-i specifies the file that contains the account data
Copyright (C) 2020 Tobias Diekershoff
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
__version__ = "0.0.1"
__usage__ = """Usage: account2txt [-v] [-h] [-i input-file-name]
-h | --help ..... displays this help
-v | --version .. displays version of the script
-i | --input .... specify the file name that should be exported
"""
import getopt
import json
import sys
class ArchiveFile(object):
"""
Class to hold the archive file data
"""
def __init__(self, filename):
"""
Constructor of the class, initialize some internal variables
and read in the data from the JSON file.
"""
with open(filename, 'r') as jason_data:
data = json.load(jason_data)
self.filename = filename
self.version = data['version']
self.schema = data['schema']
self.user = data['user']
self.profile = data['profile']
self.group = data['group']
# try:
self.profile_fields = data['profile_fields']
# except:
# self.profile_fields = None
self.photo = data['photo']
self.pconfig = data['pconfig']
self.baseurl = data['baseurl']
self.contact = data['contact']
self.group_member = data['group_member']
def to_files(self):
"""
parses the JSON data and writes the content to various text files
"""
base_file_name = self.user["nickname"]+"-"
with open(base_file_name+"info.txt", "w", encoding='utf8') as out_file:
out_file.write("Exported from Friendica Version: {}\n".format(self.version))
out_file.write("Database schema version: {}\n".format(self.schema))
out_file.write("Base URL: {}\n".format(self.baseurl))
out_file.write("Converted from: {}\n".format(self.filename))
with open(base_file_name+"user.txt", "w", encoding='utf8') as out_file:
for k in self.user:
out_file.write("{}: {}\n".format(k, self.user[k]))
with open(base_file_name+"profile.txt", "w", encoding='utf8') as out_file:
for k in self.profile[0]:
out_file.write("{}: {}\n".format(k, self.profile[0].get(k)))
with open(base_file_name+'groups.txt', "w", encoding='utf8') as out_file:
for i in range(len(self.group)):
out_file.write("Group {}\n".format(i))
for j in self.group[i]:
out_file.write("{}: {}\n".format(j, self.group[i].get(j)))
out_file.write("\n")
if self.profile_fields:
with open(base_file_name+'profile_fields.txt', 'w', encoding='utf8') as out_file:
for i in range(len(self.profile_fields)):
for j in self.profile_fields[i]:
out_file.write('{}: {}\n'.format(j, self.profile_fields[i].get(j)))
out_file.write("\n")
with open(base_file_name+'photo.txt', 'w', encoding='utf8') as out_file:
for i in range(len(self.photo)):
for j in self.photo[i]:
out_file.write('{}: {}\n'.format(j, self.photo[i].get(j)))
out_file.write("\n")
with open(base_file_name+"pconfig.txt", "w", encoding='utf8') as out_file:
for i in range(len(self.pconfig)):
for j in self.pconfig[i]:
out_file.write('{}: {}\n'.format(j, self.pconfig[i].get(j)))
out_file.write("\n")
with open(base_file_name+"contact.txt", "w", encoding='utf8') as out_file:
for i in range(len(self.contact)):
for j in self.contact[i]:
out_file.write('{}: {}\n'.format(j, self.contact[i].get(j)))
out_file.write("\n")
with open(base_file_name+"group_member.txt", "w", encoding='utf8') as out_file:
for i in range(len(self.group_member)):
for j in self.group_member[i]:
out_file.write('{}: {}\n'.format(j, self.group_member[i].get(j)))
out_file.write("\n")
def main():
"""
If the script is called directly, export the data from the supplied filename.
"""
options, arguments = getopt.getopt(sys.argv[1:], "vhi:", ["version", "help", "input="])
for o, a in options:
if o in ("-v", "--version"):
print(__version__)
sys.exit()
if o in ("-i", "--input"):
arch = ArchiveFile(filename=a)
arch.to_files()
sys.exit()
if o in ("-h", "--help"):
print(__usage__)
sys.exit()
if __name__ == "__main__":
main()