Skip to content

Commit

Permalink
Fixed foreign characters
Browse files Browse the repository at this point in the history
Added encoding to UTF-8 to solve errors with foreign characters such as Japanese ones.
  • Loading branch information
baruch95 authored Mar 26, 2019
1 parent 350474d commit aea17f2
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions PPP.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import io # character encoding
from collections import OrderedDict # url ordering
import requests # HTTP POST requests
import codecs

if not plex_token:
print('ERROR: Hmm... looks like you haven\'t set your variables! Do this by editing getPlaylists.py with a text editor')
Expand Down Expand Up @@ -118,7 +119,7 @@
print(('Saving Plex playlist: ' + str(title[0]) + '\n'))

# Get each track and save to file
file = open('tmp/plex/' + str(title[0]) + '.m3u', 'w+')
file = codecs.open('tmp/plex/' + str(title[0]) + '.m3u', 'w+', 'utf-8')

path = dom.getElementsByTagName('Part')
path = [items.attributes['file'].value for items in path] # Extract disk path to music file
Expand All @@ -141,9 +142,9 @@
for filename in os.listdir('tmp/plex/'):
if not os.path.isfile(os.path.join('tmp/local/', filename)):
print(('Found new Plex playlist: ' + filename))
plex_tracks = open(os.path.join('tmp/plex/', filename), 'r').read().splitlines()
plex_tracks = codecs.open(os.path.join('tmp/plex/', filename), 'r', 'utf-8').read().splitlines()
os.remove(os.path.join('tmp/plex/', filename))
file = open('tmp/merged/' + filename, 'w+')
file = codecs.open('tmp/merged/' + filename, 'w+', 'utf-8')
for i in range(len(plex_tracks)):
plex_tracks[i] = plex_tracks[i].strip(plex_prepend) # Strips plex_prepend
file.write(plex_tracks[i] + '\n')
Expand All @@ -152,9 +153,9 @@
for filename in os.listdir('tmp/local/'):
if not os.path.isfile(os.path.join('tmp/plex/', filename)):
print(('Found new local playlist: ' + filename))
local_tracks = open(os.path.join('tmp/local/', filename), 'r').read().splitlines()
local_tracks = codecs.open(os.path.join('tmp/local/', filename), 'r', 'utf-8').read().splitlines()
os.remove(os.path.join('tmp/local/', filename))
file = open('tmp/merged/' + filename, 'w+')
file = codecs.open('tmp/merged/' + filename, 'w+', 'utf-8')
for i in range(len(local_tracks)):
if not local_tracks[i].startswith('#'): #Skips m3u tags beginning with #
local_tracks[i] = local_tracks[i].strip(local_prepend) # Strips local_prepend
Expand All @@ -166,12 +167,12 @@

print(('Merging: ' + filename))

local_tracks = open(os.path.join('tmp/local/', filename), 'r').read().splitlines()
local_tracks = codecs.open(os.path.join('tmp/local/', filename), 'r', 'utf-8').read().splitlines()

for i in range(len(local_tracks)): # Strips local_prepend
local_tracks[i] = local_tracks[i].strip(local_prepend)

plex_tracks = open(os.path.join('tmp/plex/', filename), 'r').read().splitlines()
plex_tracks = codecs.open(os.path.join('tmp/plex/', filename), 'r', 'utf-8').read().splitlines()

for i in range(len(plex_tracks)): # Strips plex_prepend
plex_tracks[i] = plex_tracks[i].strip(plex_prepend)
Expand All @@ -193,7 +194,7 @@

# Copy merged playlists back into tmp/plex/ and tmp/local/ with prepends re-added
for filename in os.listdir('tmp/merged/'):
new_tracks = open(os.path.join('tmp/merged/', filename), 'r+').read().splitlines()
new_tracks = codecs.open(os.path.join('tmp/merged/', filename), 'r+', 'utf-8').read().splitlines()
plex_tracks = []
local_tracks = []

Expand Down

2 comments on commit aea17f2

@XDGFX
Copy link
Owner

@XDGFX XDGFX commented on aea17f2 Mar 27, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good, though is it not possible to achieve this with up io, which is also used elsewhere?

Could you please give an example of characters which are failing? It already works with some non ascii characters like ø?

@baruch95
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

笑顔の had a problem with kanji, like these ones.

Please sign in to comment.