forked from lachlan-00/rb-mergeplays
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mergeplays.py
223 lines (200 loc) · 8.7 KB
/
mergeplays.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#!/usr/bin/env python
#-*- coding: utf-8 -*-
""" Rhythmbox Merge Plays from Last.fm
----------------Authors----------------
Lachlan de Waard <[email protected]>
----------------Licence----------------
GNU General Public License version 3
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/>.
"""
import sys
import os
import shutil
import codecs
import xml
import xml.etree.ElementTree as etree
from xml.etree.ElementTree import tostring
try:
if sys.argv:
LASTFM_FILE = sys.argv[1]
else:
LASTFM_FILE = 'dump.txt'
except:
LASTFM_FILE = 'dump.txt'
LAST_FM_FILE = codecs.open(LASTFM_FILE, "r", "utf8")
LAST_FM_LIST = []
COUNT_LIST = []
ERROR_LIST = []
HOMEFOLDER = os.getenv('HOME')
PATH = '/.local/share/rhythmbox/'
DB = (HOMEFOLDER + PATH + 'rhythmdb.xml')
DBBackup = (HOMEFOLDER + PATH + 'rhythmdb-backup-merge.xml')
urlascii = ('%', "#", ';', ' ', '"', '<', '>', '?', '[', '\\',
"]", '^', '`', '{', '|', '}', '€', '‚', 'ƒ', '„',
'…', '†', '‡', 'ˆ', '‰', 'Š', '‹', 'Œ', 'Ž', '‘',
'’', '“', '”', '•', '–', '—', '˜', '™', 'š', '›',
'œ', 'ž', 'Ÿ', '¡', '¢', '£', '¥', '|', '§', '¨',
'©', 'ª', '«', '¬', '¯', '®', '¯', '°', '±', '²',
'³', '´', 'µ', '¶', '·', '¸', '¹', 'º', '»', '¼',
'½', '¾', '¿', 'À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Æ',
'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï', 'Ð',
'Ñ', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', 'Ø', 'Ù', 'Ú', 'Û',
'Ü', 'Ý', 'Þ', 'ß', 'à', 'á', 'â', 'ã', 'ä', 'å',
'æ', 'ç', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'î', 'ï',
'ð', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', '÷', 'ø', 'ù',
'ú', 'û', 'ü', 'ý', 'þ', 'ÿ', '¦')
urlcode = ('%25', '%23', '%3B', '%20', '%22', '%3C', '%3E', '%3F',
'%5B', '%5C', '%5D', '%5E', '%60', '%7B', '%7C', '%7D',
'%E2%82%AC', '%E2%80%9A', '%C6%92', '%E2%80%9E',
'%E2%80%A6', '%E2%80%A0', '%E2%80%A1', '%CB%86',
'%E2%80%B0', '%C5%A0', '%E2%80%B9', '%C5%92', '%C5%BD',
'%E2%80%98', '%E2%80%99', '%E2%80%9C', '%E2%80%9D',
'%E2%80%A2', '%E2%80%93', '%E2%80%94', '%CB%9C',
'%E2%84%A2', '%C5%A1', '%E2%80%BA', '%C5%93', '%C5%BE',
'%C5%B8', '%C2%A1', '%C2%A2', '%C2%A3', '%C2%A5',
'%7C', '%C2%A7', '%C2%A8', '%C2%A9', '%C2%AA',
'%C2%AB', '%C2%AC', '%C2%AF', '%C2%AE', '%C2%AF',
'%C2%B0', '%C2%B1', '%C2%B2', '%C2%B3', '%C2%B4',
'%C2%B5', '%C2%B6', '%C2%B7', '%C2%B8', '%C2%B9',
'%C2%BA', '%C2%BB', '%C2%BC', '%C2%BD', '%C2%BE',
'%C2%BF', '%C3%80', '%C3%81', '%C3%82', '%C3%83',
'%C3%84', '%C3%85', '%C3%86', '%C3%87', '%C3%88',
'%C3%89', '%C3%8A', '%C3%8B', '%C3%8C', '%C3%8D',
'%C3%8E', '%C3%8F', '%C3%90', '%C3%91', '%C3%92',
'%C3%93', '%C3%94', '%C3%95', '%C3%96', '%C3%98',
'%C3%99', '%C3%9A', '%C3%9B', '%C3%9C', '%C3%9D',
'%C3%9E', '%C3%9F', '%C3%A0', '%C3%A1', '%C3%A2',
'%C3%A3', '%C3%A4', '%C3%A5', '%C3%A6', '%C3%A7',
'%C3%A8', '%C3%A9', '%C3%AA', '%C3%AB', '%C3%AC',
'%C3%AD', '%C3%AE', '%C3%AF', '%C3%B0', '%C3%B1',
'%C3%B2', '%C3%B3', '%C3%B4', '%C3%B5', '%C3%B6',
'%C3%B7', '%C3%B8', '%C3%B9', '%C3%BA', '%C3%BB',
'%C3%BC', '%C3%BD', '%C3%BE', '%C3%BF', '%C2%A6')
rbdb_rep = ('%28', '%29', '%2B', '%27', '%2C', '%3A', '%21',
'%24', '%26', '%2A', '%2C', '%2D', '%2E', '%3D',
'%40', '%5F', '%7E')
rbdb_itm = ('(', ')', '+', "'", ',', ':', '!', '$', '&', '*',
',', '-', '.', '=', '@', '_', '~')
# Replace Characters with UTF code value
def set_url(string):
""" Set RhythmDB style string """
count = 0
while count < len(urlascii):
string = string.replace(urlascii[count], urlcode[count])
count = count + 1
return string
def set_ascii(string):
""" Change unicode codes back to asscii for RhythmDB """
count = 0
while count < len(rbdb_rep):
string = string.replace(rbdb_rep[count],
rbdb_itm[count])
count = count + 1
return string
print 'scanning dump file...'
# Read the last.fm list file
count = 0
for lines in LAST_FM_FILE:
#lines = set_url(lines)
lines = set_ascii(lines)
lines = lines.replace('\n', '')
lines = lines.split('\t')
lines[0] = 1
lines.pop()
lines.pop()
lines.pop()
LAST_FM_LIST.insert(count, lines)
count = count + 1
LAST_FM_FILE.close()
print 'creating db backup'
shutil.copy(DB, DBBackup)
print 'opening rhythmdb...'
root = (etree.parse(DB)).getroot()
items = root.getiterator("entry")
print 'merging dump file plays together...'
# should be converting to rhythmbox unicode... (will pull from rb-fileorganizer)
count = 0
for plays in LAST_FM_LIST:
plays[0] = 1
exist = False
for songs in COUNT_LIST:
if plays[2] == songs[2] and plays[1] == songs[1] and (
plays[3] == songs[3]):
songs[0] = songs[0] + plays[0]
exist = True
if exist == False:
COUNT_LIST.insert(count, plays)
count = count + 1
# Search the database for the song and update play-count
print str(len(COUNT_LIST)) + ' items found.'
print 'starting search for play discrepancies'
count = 0
for songs in COUNT_LIST:
for entries in items:
foundtitle = False
foundartist = False
foundalbum = False
mergeplays = False
if entries.attrib == {'type': 'song'}:
for info in entries:
if info.tag == 'title':
if (info.text).lower() == songs[1].lower():
foundtitle = True
if info.tag == 'artist':
if (info.text).lower() == songs[2].lower():
foundartist = True
if info.tag == 'album':
if (info.text).lower() == songs[3].lower():
foundalbum = True
if info.tag == 'play-count' and foundartist == True and (
foundtitle == True and foundalbum == True ):
mergeplays = True
if not int(info.text) >= int(songs[0]):
changefile = HOMEFOLDER + '/mergeplays-changes.txt'
filechanges = codecs.open(changefile,
"a", "utf8")
filechanges.write('merging: ' + songs[2] + ' - ' +
songs[1] + ' - ' + str(songs[0]) +
'\n')
info.text = str(songs[0])
filechanges.close()
# If play-count wasn't found insert it.
# Rhythmbox seems to fix indentation and location on restart
if foundartist == True and foundtitle == True and (
foundalbum == True and mergeplays == False):
mergeplays = True
insertplaycount = etree.SubElement(entries, 'play-count')
insertplaycount.text = str(songs[0])
filechanges = codecs.open(HOMEFOLDER +
'/mergeplays-changes.txt',
"a", "utf8")
filechanges.write('creating: ' + songs[2] + ' - ' + songs[1] +
' - ' + str(songs[0]) + '\n')
filechanges.close()
count = count + 1
# Every hundred files print progress
if str(count)[-2:] == '00':
print str(count) + '/' + str(len(COUNT_LIST)) + ' files processed'
#print ERROR_LIST
if mergeplays == False:
files = codecs.open(HOMEFOLDER + '/mergeplays-error.txt', "a", "utf8")
files.write(str(songs) + '\n')
files.close()
# Save changes
output = etree.ElementTree(root)
output.write((DB), encoding="utf-8")
#print ERROR_LIST
#files = codecs.open(HOMEFOLDER + '/mergeplays-error.txt', "w", "utf8")
#for items in ERROR_LIST:
# files.write(str(items) + '\n')
#files.close()
print 'done'