Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UnicodeEncodeError: 'ascii' codec can't encode character '\xa9' in position 50: ordinal not in range(128) #16

Closed
zetneteork opened this issue May 24, 2018 · 10 comments

Comments

@zetneteork
Copy link

I am getting error during downloading book.
Python 3.5.3

[-] Logging into Safari Books Online...
[*] Retrieving book info...
[-] Title: Prometheus: Up & Running
[-] Authors: Brian Brazil
[-] Identifier: 9781492034131
[-] ISBN: 9781492034148
[-] Publishers: O'Reilly Media, Inc.
--- Logging error ---
Traceback (most recent call last):
  File "/usr/lib/python3.5/logging/__init__.py", line 983, in emit
    stream.write(msg)
UnicodeEncodeError: 'ascii' codec can't encode character '\xa9' in position 50: ordinal not in range(128)
Call stack:
  File "safaribooks.py", line 1024, in <module>
    SafariBooks(args_parsed)
  File "safaribooks.py", line 292, in __init__
    self.display.book_info(self.book_info)
  File "safaribooks.py", line 135, in book_info
    self.info("{0}{1}{2}: {3}".format(self.SH_YELLOW, t[0], self.SH_DEFAULT, t[1]), True)
  File "safaribooks.py", line 67, in info
    self.log(message)
  File "safaribooks.py", line 61, in log
    self.logger.info(str(message))
Message: "\x1b[33mRights\x1b[0m: Copyright \xa9 O'Reilly Media, Inc."
Arguments: ()
[#] Unhandled Exception: 'ascii' codec can't encode character '\xa9' in position 239: ordinal not in range(128) (type: UnicodeEncodeError)
[+] Please delete all the `<BOOK NAME>/OEBPS/*.xhtml` files and restart the program.
[!] Aborting...
--- Logging error ---
Traceback (most recent call last):
  File "/usr/lib/python3.5/logging/__init__.py", line 983, in emit
    stream.write(msg)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 6722-6723: ordinal not in range(128)
Call stack:
  File "safaribooks.py", line 95, in unhandled_exception
    self.exit("Unhandled Exception: %s (type: %s)" % (o, o.__class__.__name__))
  File "safaribooks.py", line 90, in exit
    self.save_last_request()
  File "safaribooks.py", line 100, in save_last_request
    .format(*self.last_request))
  File "safaribooks.py", line 61, in log
    self.logger.info(str(message))
Message: 'Last request done:\n\tURL: https://www.safaribooksonline.com/api/v1/book/9781492034131/\n\tDATA: None\n\tOTHERS: {}\n\n\t200\n\tServer: nginx/1.10.3 (Ubuntu)\n\tContent-Type: application/json\n\tAllow: GET, HEAD, OPTIONS\n\tx-xss-protection: 1; mode=block\n\tContent-Language: en-US\n\tx-content-type-options: nosniff\n\tX-Frame-Options: SAMEORIGIN\n\tstrict-transport-security: max-age=3600; includeSubDomains, max-age=31536000; includeSubdomains\n\tContent-Encoding: gzip\n\tCache-Control: s-maxage=31536000\n\tContent-Length: 1711\n\tAccept-Ranges: bytes\n\tDate: Thu, 24 May 2018 21:25:45 GMT\n\tVia: 1.1 varnish\n\tConnection: keep-alive\n\tX-Client-IP: 62.245.87.212\n\tX-Served-By: cache-hhn1541-HHN\n\tX-Cache: MISS\n\tX-Cache-Hits: 0\n\tX-Timer: S1527197145.328775,VS0,VE551\n\tVary: Accept-Encoding\n\n{"url":"https://www.safaribooksonline.com/api/v1/book/9781492034131/","natural_key":["9781492034131"],"authors":[{"name":"Brian Brazil"}],"subjects":[],"topics":[{"score":-0.692030336285675,"name":"Information Technology / Operations","slug":"information-technology-operations","uuid":"c363ab45-daa6-442e-b572-1a4467aceeb0","epub_identifier":"9781492034131"}],"publishers":[{"name":"O\'Reilly Media, Inc.","id":1,"slug":"oreilly-media-inc"}],"chapters":["https://www.safaribooksonline.com/api/v1/book/9781492034131/chapter/cover.html","https://www.safaribooksonline.com/api/v1/book/9781492034131/chapter/toc01.html","https://www.safaribooksonline.com/api/v1/book/9781492034131/chapter/titlepage01.html","https://www.safaribooksonline.com/api/v1/book/9781492034131/chapter/copyright-page01.html","https://www.safaribooksonline.com/api/v1/book/9781492034131/chapter/preface01.html","https://www.safaribooksonline.com/api/v1/book/9781492034131/chapter/part01.html","https://www.safaribooksonline.com/api/v1/book/9781492034131/chapter/ch01.html","https://www.safaribooksonline.com/api/v1/book/9781492034131/chapter/ch02.html","https://www.safaribooksonline.com/api/v1/book/9781492034131/chapter/part02.html","https://www.safaribooksonline.com/api/v1/book/9781492034131/chapter/ch03.html","https://www.safaribooksonline.com/api/v1/book/9781492034131/chapter/ch04.html","https://www.safaribooksonline.com/api/v1/book/9781492034131/chapter/ch05.html","https://www.safaribooksonline.com/api/v1/book/9781492034131/chapter/ch06.html","https://www.safaribooksonline.com/api/v1/book/9781492034131/chapter/part03.html","https://www.safaribooksonline.com/api/v1/book/9781492034131/chapter/ch07.html","https://www.safaribooksonline.com/api/v1/book/9781492034131/chapter/ch08.html","https://www.safaribooksonline.com/api/v1/book/9781492034131/chapter/ch09.html","https://www.safaribooksonline.com/api/v1/book/9781492034131/chapter/ch10.html","https://www.safaribooksonline.com/api/v1/book/9781492034131/chapter/ch11.html","https://www.safaribooksonline.com/api/v1/book/9781492034131/chapter/ch12.html","https://www.safaribooksonline.com/api/v1/book/9781492034131/chapter/part04.html","https://www.safaribooksonline.com/api/v1/book/9781492034131/chapter/ch13.html","https://www.safaribooksonline.com/api/v1/book/9781492034131/chapter/ch14.html","https://www.safaribooksonline.com/api/v1/book/9781492034131/chapter/ch15.html","https://www.safaribooksonline.com/api/v1/book/9781492034131/chapter/ch16.html","https://www.safaribooksonline.com/api/v1/book/9781492034131/chapter/ch17.html","https://www.safaribooksonline.com/api/v1/book/9781492034131/chapter/part05.html","https://www.safaribooksonline.com/api/v1/book/9781492034131/chapter/ch18.html","https://www.safaribooksonline.com/api/v1/book/9781492034131/chapter/ch19.html","https://www.safaribooksonline.com/api/v1/book/9781492034131/chapter/part06.html","https://www.safaribooksonline.com/api/v1/book/9781492034131/chapter/ch20.html","https://www.safaribooksonline.com/api/v1/book/9781492034131/chapter/ix01.html","https://www.safaribooksonline.com/api/v1/book/9781492034131/chapter/colophon01.html","https://www.safaribooksonline.com/api/v1/book/9781492034131/chapter/colophon02.html"],"cover":"https://www.safaribooksonline.com/library/cover/9781492034131/","chapter_list":"https://www.safaribooksonline.com/api/v1/book/9781492034131/chapter/","toc":"https://www.safaribooksonline.com/api/v1/book/9781492034131/toc/","flat_toc":"https://www.safaribooksonline.com/api/v1/book/9781492034131/flat-toc/","web_url":"https://www.safaribooksonline.com/library/view/prometheus-up/9781492034131/","last_chapter_read":{"url":"https://www.safaribooksonline.com/api/v1/book/9781492034131/chapter/ch09.html","title":"9. Containers and Kubernetes","web_url":"https://www.safaribooksonline.com/library/view/prometheus-up/9781492034131/ch09.html"},"academic_excluded":false,"opf_unique_identifier_type":"pub-identifier","has_mathml":false,"created_time":"2018-05-01T16:47:13.772384Z","last_modified_time":"2018-05-01T16:53:31.924780Z","identifier":"9781492034131","name":"book.epub","title":"Prometheus: Up & Running","format":"book","content_format":"book","source":"application/epub+zip","orderable_title":"Prometheus: Up & Running","has_stylesheets":true,"description":"<span><div><p>Get up to speed with Prometheus, the metrics-based monitoring system used by thousands of organizations in production. This practical guide provides application developers, sysadmins, and DevOps practitioners with a hands-on introduction to the important aspects of Prometheus, including infrastructure and application monitoring, dashboarding and alerting, direct code instrumentation, and metric collection from third-party systems with exporters.</p><p>This open source system has gained popularity over the past few years for good reason. With its simple yet powerful data model and query language, Prometheus does one thing and it does it well. Author and Prometheus core developer Brian Brazil guides you through Prometheus setup, the Node Exporter, and the Alertmanager, then guides you through its use in application and infrastructure monitoring.</p><ul><li>Know where and how much to apply instrumentation to your application code</li><li>Expose metrics through client libraries to make them available to Prometheus</li><li>Identify metrics with labels: unique key-value pairs associated with a time series</li><li>Get an introduction to Grafana, a popular tool for building dashboards</li><li>Learn how to use the node exporter to monitor your infrastructure</li><li>Use service discovery to provide different views of your machines and services</li><li>Use Prometheus with Kubernetes, and examine exporters you can use with containers</li><li>Convert data from other monitoring systems into the Prometheus format</li></ul></div></span>","isbn":"9781492034148","issued":"2018-07-15","language":"en","rights":"Copyright \xc2\xa9 O\'Reilly Media, Inc.","updated":"2018-05-01T16:40:44.367564Z","orderable_author":"Brazil, Brian","purchase_link":null,"publisher_resource_links":{"Errata Page":"https://oreilly.com/catalog/0636920147343/errata"},"is_free":false,"is_system_book":true,"is_active":true,"is_hidden":false,"virtual_pages":458,"duration_seconds":null,"pagecount":384}\n'
Arguments: ()

info_9781492033905.log  info_9781492034131.log
lukas@PRGN00008351A:~/git/safaribooks$ cat info_9781492034131.log
[24/May/2018 23:22:14] ** Welcome to SafariBooks! **
[24/May/2018 23:22:14] Logging into Safari Books Online...
[24/May/2018 23:22:20] Retrieving book info...
[24/May/2018 23:22:21] Title: Prometheus: Up & Running
[24/May/2018 23:22:21] Authors: Brian Brazil
[24/May/2018 23:22:21] Identifier: 9781492034131
[24/May/2018 23:22:21] ISBN: 9781492034148
[24/May/2018 23:22:21] Publishers: O'Reilly Media, Inc.
[24/May/2018 23:22:21]   File "safaribooks.py", line 1024, in <module>
    SafariBooks(args_parsed)
  File "safaribooks.py", line 292, in __init__
    self.display.book_info(self.book_info)
  File "safaribooks.py", line 135, in book_info
    self.info("{0}{1}{2}: {3}".format(self.SH_YELLOW, t[0], self.SH_DEFAULT, t[1]), True)
  File "safaribooks.py", line 70, in info
    self.out(output)
  File "safaribooks.py", line 64, in out
    sys.stdout.write("\r" + " " * self.columns + "\r" + put + "\n")

[24/May/2018 23:22:21] Unhandled Exception: 'ascii' codec can't encode character '\xa9' in position 122: ordinal not in range(128) (type: UnicodeEncodeError)
[24/May/2018 23:25:43] ** Welcome to SafariBooks! **
[24/May/2018 23:25:43] Logging into Safari Books Online...
[24/May/2018 23:25:45] Retrieving book info...
[24/May/2018 23:25:46] Title: Prometheus: Up & Running
[24/May/2018 23:25:46] Authors: Brian Brazil
[24/May/2018 23:25:46] Identifier: 9781492034131
[24/May/2018 23:25:46] ISBN: 9781492034148
[24/May/2018 23:25:46] Publishers: O'Reilly Media, Inc.
[24/May/2018 23:25:46]   File "safaribooks.py", line 1024, in <module>
    SafariBooks(args_parsed)
  File "safaribooks.py", line 292, in __init__
    self.display.book_info(self.book_info)
  File "safaribooks.py", line 135, in book_info
    self.info("{0}{1}{2}: {3}".format(self.SH_YELLOW, t[0], self.SH_DEFAULT, t[1]), True)
  File "safaribooks.py", line 70, in info
    self.out(output)
  File "safaribooks.py", line 64, in out
    sys.stdout.write("\r" + " " * self.columns + "\r" + put + "\n")

[24/May/2018 23:25:46] Unhandled Exception: 'ascii' codec can't encode character '\xa9' in position 239: ordinal not in range(128) (type: UnicodeEncodeError)

@zetneteork
Copy link
Author

Can I support you with any kind of more details?

@idoan
Copy link

idoan commented Jun 4, 2018

I haven't used this script but the solution to that error is this:

https://stackoverflow.com/questions/9942594/unicodeencodeerror-ascii-codec-cant-encode-character-u-xa0-in-position-20

so, briefly call .encode('utf-8') method for the string the code is printing.

replace this:

sys.stdout.write("\r" + " " * self.columns + "\r" + put + "\n")
with this:
sys.stdout.write("\r" + " " * self.columns + "\r" + put.encode('utf-8') + "\n")

and this one:

self.logger.info(str(message))

with this one:

self.logger.info(message.encode('utf-8'))

I hope this works

@ghost
Copy link

ghost commented Oct 24, 2018

I have the same error:

[#] Unhandled Exception: 'ascii' codec can't encode character '\xa9' in position 241: ordinal not in range(128) (type: UnicodeEncodeError)
[+] Please delete all the <BOOK NAME>/OEBPS/*.xhtml files and restart the program.
[!] Aborting...

Yesterday everything worked. I don't know what happened today

@fmontealegre
Copy link

Hi Lorenzo thanks for the tool,
I'm getting the same error:

[*] Retrieving book info...
[-] Title: Test-Driven Development with Python, 2nd Edition
[-] Authors: Harry J.W. Percival
[-] Identifier: 9781491958698
[-] ISBN: 9781491958704
[-] Publishers: O'Reilly Media, Inc.
[#] Unhandled Exception: 'ascii' codec can't encode character '\xa9' in position 206: ordinal not in range(128) (type: UnicodeEncodeError)
[+] Please delete all the <BOOK NAME>/OEBPS/*.xhtml files and restart the program.
[!] Aborting...

Log
[13/Feb/2019 04:22:17] ** Welcome to SafariBooks! **
[13/Feb/2019 04:22:17] b'Retrieving book info...'
[13/Feb/2019 04:22:19] b'\x1b[33mTitle\x1b[0m: Test-Driven Development with Python, 2nd Edition'
[13/Feb/2019 04:22:19] b'\x1b[33mAuthors\x1b[0m: Harry J.W. Percival'
[13/Feb/2019 04:22:19] b'\x1b[33mIdentifier\x1b[0m: 9781491958698'
[13/Feb/2019 04:22:19] b'\x1b[33mISBN\x1b[0m: 9781491958704'
[13/Feb/2019 04:22:19] b"\x1b[33mPublishers\x1b[0m: O'Reilly Media, Inc."
[13/Feb/2019 04:22:19] b"\x1b[33mRights\x1b[0m: Copyright \xc2\xa9 O'Reilly Media, Inc."
[13/Feb/2019 04:22:19] b' File "safaribooks.py", line 1025, in \n SafariBooks(args_parsed)\n File "safaribooks.py", line 295, in init\n self.dis
play.book_info(self.book_info)\n File "safaribooks.py", line 138, in book_info\n self.info("{0}{1}{2}: {3}".format(self.SH_YELLOW, t[0], self.SH_DEFAULT, t[1]),
True)\n File "safaribooks.py", line 73, in info\n self.out(output)\n File "safaribooks.py", line 67, in out\n sys.stdout.write("\r" + " " * self.columns +
"\r" + put + "\n")\n'
[13/Feb/2019 04:22:19] b"Unhandled Exception: 'ascii' codec can't encode character '\xa9' in position 206: ordinal not in range(128) (type: UnicodeEncodeError)"

Please let us know if more info is required.

@fmontealegre
Copy link

I pulled the new version now I'm getting the following error:
Is it working for you?

python3 safaribooks.py 9781491958698
Error in sys.excepthook:
Traceback (most recent call last):
File "safaribooks.py", line 98, in unhandled_exception
self.exit("Unhandled Exception: %s (type: %s)" % (o, o.class.name))
File "safaribooks.py", line 84, in exit
self.error(str(error))
File "safaribooks.py", line 81, in error
self.out(output)
File "safaribooks.py", line 67, in out
sys.stdout.write("\r" + " " * self.columns + "\r" + put.encode("utf-8", "replace") + "\n")
TypeError: Can't convert 'bytes' object to str implicitly

Original exception was:
Traceback (most recent call last):
File "safaribooks.py", line 1026, in
SafariBooks(args_parsed)
File "safaribooks.py", line 274, in init
self.display.intro()
File "safaribooks.py", line 117, in intro
self.out(output)
File "safaribooks.py", line 67, in out
sys.stdout.write("\r" + " " * self.columns + "\r" + put.encode("utf-8", "replace") + "\n")
TypeError: Can't convert 'bytes' object to str implicitly

@fmontealegre
Copy link

Please reopen

Now I'm getting the following error, is this solution working for you?

python3 safaribooks.py 9781491958698
Error in sys.excepthook:
Traceback (most recent call last):
File "safaribooks.py", line 98, in unhandled_exception
self.exit("Unhandled Exception: %s (type: %s)" % (o, o.class.name))
File "safaribooks.py", line 84, in exit
self.error(str(error))
File "safaribooks.py", line 81, in error
self.out(output)
File "safaribooks.py", line 67, in out
sys.stdout.write("\r" + " " * self.columns + "\r" + put.encode("utf-8", "replace") + "\n")
TypeError: Can't convert 'bytes' object to str implicitly

Original exception was:
Traceback (most recent call last):
File "safaribooks.py", line 1026, in
SafariBooks(args_parsed)
File "safaribooks.py", line 274, in init
self.display.intro()
File "safaribooks.py", line 117, in intro
self.out(output)
File "safaribooks.py", line 67, in out
sys.stdout.write("\r" + " " * self.columns + "\r" + put.encode("utf-8", "replace") + "\n")
TypeError: Can't convert 'bytes' object to str implicitly

@danilohgds
Copy link

danilohgds commented Feb 14, 2019

@lorenzodifuccia heres the log from the issue i opened:
https://pastebin.com/D09jjaxh

@dexter101010
Copy link

@lorenzodifuccia heres the log from the issue i opened:
https://pastebin.com/D09jjaxh

It is work like a charm. Already downloaded the book...

@toanalien
Copy link

Try export LC_ALL='en_US.utf8'

vlqhai pushed a commit to vlqhai/safaribooks that referenced this issue Feb 23, 2020
@damianbento
Copy link

Try export LC_ALL='en_US.utf8'

It works !!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants