ArdaxHz / mdownloader

MangaDex downloader using python. Forked from the repo linked
https://gitlab.com/hitoribocchi/mdownloader
GNU General Public License v3.0
9 stars 1 forks source link

Force UTF8 for json #9

Closed NLZ closed 3 years ago

NLZ commented 3 years ago
Traceback (most recent call last):
  File "C:\Users\nlz\Code\mdownloader\mdownloader.py", line 123, in <module>
    beforeMain(args.id, args.language, args.directory, args.type, args.save_format, args.folder, args.covers)
  File "C:\Users\nlz\Code\mdownloader\mdownloader.py", line 105, in beforeMain
    main(id, language, directory, type, save_format, folder, covers)
  File "C:\Users\nlz\Code\mdownloader\components\main.py", line 81, in main
    bulkDownloader(id, language, route, type, save_format, make_folder, covers)
  File "C:\Users\nlz\Code\mdownloader\components\main.py", line 65, in bulkDownloader
    typeChecker(id, language, route, type, save_format, make_folder, covers)
  File "C:\Users\nlz\Code\mdownloader\components\main.py", line 18, in typeChecker
    downloadBatch(id, language, route, type, save_format, make_folder, covers)
  File "C:\Users\nlz\Code\mdownloader\components\downloader.py", line 227, in downloadBatch
    downloadChapter(chapter_id, title, route, type, save_format, make_folder, json_file)
  File "C:\Users\nlz\Code\mdownloader\components\downloader.py", line 158, in downloadChapter
    json_file.core(0)
  File "C:\Users\nlz\Code\mdownloader\components\jsonmaker.py", line 249, in core
    self.saveJson()
  File "C:\Users\nlz\Code\mdownloader\components\jsonmaker.py", line 101, in saveJson
    json.dump(self.new_data, json_file, indent=4, ensure_ascii=False)
  File "C:\Python39\lib\json\__init__.py", line 180, in dump
    fp.write(chunk)
  File "C:\Python39\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 1-3: character maps to <undefined>

Here it fails at writing author of https://mangadex.org/api/v2/title/9012?saver=0, which is in korean. Looking at the error closely it says cp1252, and I think you cannot do korean characters in this encoding.

I think the best solution would be to force UTF8, so here: https://github.com/Rudoal/mdownloader/blob/8164e67d793838810734871cf6e19298fd094b99/components/jsonmaker.py#L100-L101 instead do:

with open(self.json_path, 'w', encoding='utf8') as json_file:

Makes me think that it would've solved Issue #6

ArdaxHz commented 3 years ago

Fixed in upcoming v2.7.4, if you find more errors hit me with them before I push the update.