python scripts to convert Friendica accounts and backup files to text files
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

162 lines
6.8 KiB

#!/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(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 in_file:
lines = in_file.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 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")
for item in self.item:
item_file_name = base_file_name + "item-" + item['created'].replace(' ', '_') + '-Title_' + str(item['title']).replace(' ', '_') + '-' + item['guid'] + '.txt'
with open(item_file_name, 'w', encoding='utf8') as out_file:
out_file.write("# Title: {}\n".format(str(item['title'])))
out_file.write("# Created: {}\n".format(str(item['created'])))
out_file.write("# ID: {}\n".format(str(item['id'])))
out_file.write("# guid: {}\n".format(str(item['guid'])))
out_file.write("# contact-id: {}\n".format(str(item['contact-id'])))
out_file.write("# gcontact-id : {}\n".format(str(item['gcontact-id'])))
out_file.write("# parent ID: {}\n".format(str(item['parent'])))
out_file.write("# edited: {}\n".format(str(item['edited'])))
out_file.write(str(item['body']))
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 opt, a in options:
if opt in ("-v", "--version"):
print(__version__)
sys.exit()
if opt in ("-i", "--input"):
arch = ArchiveFile(filename=a)
arch.to_files()
sys.exit()
if opt in ("-h", "--help"):
print(__usage__)
sys.exit()
if __name__ == "__main__":
main()