-2

An error occurred when compiling "QRCODE.py"

from pyshorteners import Shortener

class Shortening():
    def __init__(self):
        self.shortener=Shortener('Tinyurl')
        fo = open('/home/jayu/Desktop/qr.png','r+')
        apiKey = fo.read()
        self.shortener = Shortener('Google',api_key = apiKey)
    def shortenURL(self):
        self.url = raw_input("Enter The Url to shortener : ");
        shortener = self.shortener.short(self.url)
        print ("the short url : " +shortenURL) 
    def decodeURL(self):
        self.url = raw_input("Enter The Url to expand: ");
        expandURL = self.shortener.expand(self.url)
        print ("the short url : " +expandURL);
    def generateQRcode(self):
        self.url = raw_input("Enter the URL to get QR code :")
        self.shortener.short(self.url)
        print (self.shortener.qrcode(150,150))
app = Shortening()
option = int (input("Enter ur choice : "))

if option==1:
    app.shortenURL()
elif option==2:
    decodeURL()
elif option==3:
    app.generateQRcode()
else:
    print ("wrong ")

Traceback (most recent call last):

jayu@jayu:~/Desktop$ python QRCODE.py 
Enter ur choice : 3
Enter the URL to get QR code :http://www.google.com
Traceback (most recent call last):
  File "QRCODE.py", line 29, in <module>
    app.generateQRcode()
  File "QRCODE.py", line 19, in generateQRcode
    self.shortener.short(self.url)
  File "/home/jayu/.local/lib/python2.7/site-packages/pyshorteners/shorteners/__init__.py", line 115, in short
    self.shorten = self._class(**self.kwargs).short(url)
  File "/home/jayu/.local/lib/python2.7/site-packages/pyshorteners/shorteners/googl.py", line 25, in short
    response = self._post(url, data=params, headers=headers)
  File "/home/jayu/.local/lib/python2.7/site-packages/pyshorteners/shorteners/base.py", line 32, in _post
    timeout=self.kwargs['timeout'])
  File "/home/jayu/.local/lib/python2.7/site-packages/requests/api.py", line 112, in post
    return request('post', url, data=data, json=json, **kwargs)
  File "/home/jayu/.local/lib/python2.7/site-packages/requests/api.py", line 58, in request
    return session.request(method=method, url=url, **kwargs)
  File "/home/jayu/.local/lib/python2.7/site-packages/requests/sessions.py", line 498, in request
    prep = self.prepare_request(req)
  File "/home/jayu/.local/lib/python2.7/site-packages/requests/sessions.py", line 441, in prepare_request
    hooks=merge_hooks(request.hooks, self.hooks),
  File "/home/jayu/.local/lib/python2.7/site-packages/requests/models.py", line 309, in prepare
    self.prepare_url(url, params)
  File "/home/jayu/.local/lib/python2.7/site-packages/requests/models.py", line 359, in prepare_url
    url = url.decode('utf8')
  File "/usr/lib/python2.7/encodings/utf_8.py", line 16, in decode
    return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode byte 0x89 in position 51: invalid start byte

What is the cause of the error? Python's version is 2.7.15rc1 Each time I tried to run python QRCODE.py I got a same position N in the traceback.

can anyone correct me ?

Jay Suthar
  • 199
  • 1
  • 4
  • 14
  • You say “Traceback” but I don't see any traceback. Where did the traceback go? – Dietrich Epp Jul 27 '18 at 16:23
  • 1
    It looks like `Shortener('Google',api_key = apiKey)` is expecting a string as `api_key`, but you appear to be passing the contents of a png file? What's going on there? – Alastair McCormack Jul 27 '18 at 16:42
  • 1
    PNG files always start with `b'\x89PNG\r\n\x1a\n'`, which is not a valid UTF-8 string, hence the error. If you do need to read a PNG file, then read it in binary mode, not text mode. But why are you passing PNG data as an "API key"? – dan04 Jul 27 '18 at 17:03

1 Answers1

-2

If you have this problem in open(...) function you need to set encoding in this function

fo = open(filename, something_else, encoding = 'UTF-8')

but it's work only in python3 in python 2 you need to use io.open:

fo = io.open(filename, something else, encoding = 'UTF-8') 

go to google i don't know full sintax, but i already answered alike ask here: unable to decode this string using python