I'm making an image grabber for the puush service; however, whenever I generate a random URL and attempt to verify it as a .PNG image, an error is generated. I just took up the Python language earlier today, so you could say I'm very new to this!
The error that is generated:
Traceback (most recent call last):
File "run.py", line 19, in <module>
extension = guess_extension(guess_type(url))
File "C:\Python33\lib\mimetypes.py", line 320, in guess_extension
return _db.guess_extension(type, strict)
File "C:\Python33\lib\mimetypes.py", line 189, in guess_extension
extensions = self.guess_all_extensions(type, strict)
File "C:\Python33\lib\mimetypes.py", line 168, in guess_all_extensions
type = type.lower()
AttributeError: 'tuple' object has no attribute 'lower'
The code that is ran:
#!/usr/bin/env python
import sys
import urllib
from mimetypes import guess_type, guess_extension
from random import choice
randoms = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',' J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
downloads = 1;
#the number of files we want to download
target = int(sys.argv[1]);
while downloads <= target:
string = choice(randoms)+choice(randoms)+choice(randoms)+choice(randoms)+choice(randoms)
url = 'http://puu.sh/'+string
print(str(downloads)+': '+string)
#download
extension = guess_extension(guess_type(url))
print(extension)
#urllib.request('http://puu.sh/'+string, string+'.png')
downloads += 1
Any ideas on what this error is attempting to tell me? Thanks.
guess_type() returns a tuple (type,encoding)
, whereas guess_extension()
accepts a single argument type
.
The line
extension = guess_extension(guess_type(url))
calls guess_type
and then passes its return value (tuple) to guess_extension
. You should only pass the first element of the tuple (type
)
extension = guess_extension(guess_type(url)[0])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With