moved the code into src/ and added LICENSE file
[mevemon] / src / fetchimg.py
1 import urllib
2 import os.path
3
4 def portrait_filename( char_id, img_size ):
5
6     err_img = 'imgs/error.jpg'
7
8     # we can only accept 64 or 256... I know an exclamation point is not an error message, but I'll come back to this. FIXME --danny
9     if not ( img_size == 64 or img_size == 256 ):
10         return err_img
11
12     # if asked for the large version, save it under a diff name --danny
13     if img_size == 64:
14         filename = "imgs/%s.jpg" % char_id
15     elif img_size == 256:
16         filename = "imgs/%s_lg.jpg" % char_id
17
18     if os.path.isfile( filename ):
19         return filename
20
21     # specify size and cid --danny
22     img_url = "http://img.eve.is/serv.asp?s=%s&c=%s" % ( str( img_size ), char_id )
23
24     # fetch it, and hit the road. --danny
25     try:
26         urllib.urlretrieve( img_url, filename, report_handler )
27     except ContentTooShortError:
28         filename = err_img
29     return filename
30
31 def report_handler( *a ):
32     ( blocks_transferred, block_size, total_size ) = a