archive2txt/account2txt.py

132 lines
5 KiB
Python
Raw Normal View History

#!/usr/bin/env python
# -*- 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:
"""
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) as jsonData:
data = json.load(jsonData)
self.filename = filename
self.version = data['version']
self.schema = data['schema']
self.user = data['user']
self.profile = data['profile']
self.group = data['group']
self.profile_fields = data['profile_fields']
self.photo = data['photo']
self.pconfig = data['pconfig']
self.baseurl = data['baseurl']
self.contact = data['contact']
self.group_member = data['group_member']
def toFiles(self):
"""
parses the JSON data and writes the content to various text files
"""
baseFileName = self.user["nickname"]+"-"
with open(baseFileName+"info.txt", "w") as outFile:
outFile.write("Exported from Friendica Version: {}\n".format(self.version))
outFile.write("Database schema version: {}\n".format(self.schema))
outFile.write("Base URL: {}\n".format(self.baseurl))
with open(baseFileName+"userdata.txt", "w") as outFile:
for k in self.user:
outFile.write("{}: {}\n".format(k, self.user[k]))
with open(baseFileName+"profile.txt", "w") as outFile:
for k in self.profile[0]:
outFile.write("{}: {}\n".format(k, self.profile[0].get(k)))
with open(baseFileName+'groups.txt', "w") as outFile:
for i in range(len(self.group)):
outFile.write("Group {}\n".format(i))
for ii in self.group[i]:
outFile.write("{}: {}\n".format(ii, self.group[i].get(ii)))
outFile.write("\n")
with open(baseFileName+'profile_fields.txt', 'w') as outFile:
for i in range(len(self.profile_fields)):
for ii in self.profile_fields[i]:
outFile.write('{}: {}\n'.format(ii, self.profile_fields[i].get(ii)))
outFile.write("\n")
with open(baseFileName+'photo.txt', 'w') as outFile:
for i in range(len(self.photo)):
for ii in self.photo[i]:
outFile.write('{}: {}\n'.format(ii, self.photo[i].get(ii)))
outFile.write("\n")
with open(baseFileName+"pconfig.txt","w") as outFile:
for i in range(len(self.pconfig)):
for ii in self.pconfig[i]:
outFile.write('{}: {}\n'.format(ii, self.pconfig[i].get(ii)))
outFile.write("\n")
with open(baseFileName+"contact.txt","w") as outFile:
for i in range(len(self.contact)):
for ii in self.contact[i]:
outFile.write('{}: {}\n'.format(ii, self.contact[i].get(ii)))
outFile.write("\n")
with open(baseFileName+"group_member.txt","w") as outFile:
for i in range(len(self.group_member)):
for ii in self.group_member[i]:
outFile.write('{}: {}\n'.format(ii, self.group_member[i].get(ii)))
outFile.write("\n")
if __name__ == "__main__":
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.toFiles()
sys.exit()
if o in ("-h", "--help"):
print(__usage__)
sys.exit()