added initial backup2txt.py

This commit is contained in:
Tobias Diekershoff 2020-09-26 17:50:24 +02:00
parent 3033fdce2c
commit b1ba10688a
1 changed files with 158 additions and 0 deletions

158
backup2txt.py Executable file
View File

@ -0,0 +1,158 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Friendica backup2txt
This script will take an exported account file from Friendica and
convert it into a couple of text files that contain the information.
Usage:
backup2txt [-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: backup2txt [-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, 'r') as inFile:
lines = inFile.readlines()
# in the 0th line there are the account file information
data = json.loads(lines[0])
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']
# in all the other lines, the postings are stored
data = ""
for i in lines[1:]:
data += i
data = json.loads(data)
self.item = data['item']
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", encoding='utf8') 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))
outFile.write("Converted from: {}\n".format(self.filename))
with open(baseFileName+"user.txt", "w", encoding='utf8') as outFile:
for k in self.user:
outFile.write("{}: {}\n".format(k, self.user[k]))
with open(baseFileName+"profile.txt", "w", encoding='utf8') as outFile:
for k in self.profile[0]:
outFile.write("{}: {}\n".format(k, self.profile[0].get(k)))
with open(baseFileName+'groups.txt', "w", encoding='utf8') 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")
if self.profile_fields:
with open(baseFileName+'profile_fields.txt', 'w', encoding='utf8') 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', encoding='utf8') 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", encoding='utf8') 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", encoding='utf8') 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", encoding='utf8') 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")
for item in self.item:
itemFileName = baseFileName + "item-" + item['created'].replace(' ', '_') + '-Title_' + str(item['title']).replace(' ','_') + '-' + item['guid'] + '.txt'
with open(itemFileName, 'w', encoding='utf8') as outFile:
outFile.write("# Title: {}\n".format(str(item['title'])))
outFile.write("# Created: {}\n".format(str(item['created'])))
outFile.write("# ID: {}\n".format(str(item['id'])))
outFile.write("# guid: {}\n".format(str(item['guid'])))
outFile.write("# contact-id: {}\n".format(str(item['contact-id'])))
outFile.write("# gcontact-id : {}\n".format(str(item['gcontact-id'])))
outFile.write("# parent ID: {}\n".format(str(item['parent'])))
outFile.write("# edited: {}\n".format(str(item['edited'])))
outFile.write(str(item['body']))
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()