Wpis z mikrobloga

#android #windowsphone #python

Krótki skrypt w Python do konwersji pliku wiadomości z formatu Windows Phone do formatu xml dla Androida. Troszkę go poprawiłem w celu wyeliminowania podwójnych wpisów. Mam nadzieję, że komuś się przyda.

#Deklaracje modułów
from __future__ import division
import xml.etree.ElementTree as ET

import os
import sys

if (sys.version_info >= (3, 0)):
import tkinter as Tkinter
import tkinter.filedialog as tkFileDialog
isPy3 = True
else:
import Tkinter
import tkFileDialog
isPy3 = False

# Okno dialogowe
Tkinter.Tk().withdraw()
currdir = os.getcwd()
file_path = tkFileDialog.askopenfilename(initialdir=currdir, filetypes =(("Messages file", "*.msg"),("All Files","*.*")), title='Proszę wybrać plik wiadomości *.msg')

e = ET.parse(file_path).getroot()

messages = e.findall('Message')
total_count = len(messages)

output_template = 'xml version=\'1.0\' encoding=\'UTF-8\' standalone=\'yes\' ?\n\n{content}'
line_template = '\n'
content = ''
lines_temp = ''

print('Suma wiadomości w pliku: ' + str(total_count) + '\n')

i = 0
for m in messages:
i += 1
postep = (i / total_count) * 100

sys.stdout.write('\r')
sys.stdout.write("\n[%-20s] ilość wiadomości: %d" % ('=' * int(postep / 5), i))
sys.stdout.flush()

# Tekst wiadomości
text = m.find('Body').text
if text is not None:
body = text.replace("\"", """)
if not isPy3:
body = body.encode('utf-8', 'ignore')
else:
# Pusty string gdyby jednak miadomość nie miała treści
body = ''

# Typ wiadomości --> 1=odebrane, 2=wysłane
type = '1' if m.find('IsIncoming').text == 'true' else '2'

# Wiadomość odebrana
if type == '1':
address = m.find('Sender').text
# Wiadomość wysłana
elif type == '2':
address = list(m.find('Recepients'))[0].text
# pusty string adresu
else:
address = ''

if address is not None and address != '' and not isPy3:
address = address.encode('utf-8', 'ignore')

# Obróbka timestamp na postać UNIX
ts = int(m.find('LocalTimestamp').text) / 10000 - 11644473600000

line = line_template.format(
address=address,
timestamp=ts,
type=type,
body=body
)

if lines_temp != line:
content += line
lines_temp = line
output = output_template.format(
count=total_count,
content=content)

path = os.path.dirname(os.path.abspath(file_path))

if isPy3:
with open(os.path.join(path, 'wp_messages.xml'), 'w', encoding='utf-8') as file:
file.write(output)
else:
with open(os.path.join(path, 'wp_messages.xml'), 'w') as file:
file.write(output)

print('\n\nPlik wyjściowy --> wp_messages.xml')
  • Odpowiedz
  • Otrzymuj powiadomienia
    o nowych komentarzach