I would do:

def getImage(url,db): 
    user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
    headers = {'User-Agent': user_agent}
    
    req = urllib2.Request(url,"",headers)
    
    try:
        response = urllib2.urlopen(req)
    except urllib2.URLError, e:
        if hasattr(e, 'reason'):
            print 'We failed to reach a server.'
            print 'Reason: ', e.reason
        elif hasattr(e, 'code'):
            print 'The server couldn\'t fulfill the request.'
            print 'Error code: ', e.code
    else:
        pass # everything is fine
    print response.geturl()
    print response.info()
    # print response.read() # <<< do not read it
    # f = open(response.read(),'rb') # << response is already a stream
    # stream = open(response,'rb')
    id = 
db.image.insert(url=url,image=db.image.image_upload.store(response,filename=url))
    db.commit() # not sure if needed, depends on where executed
    return id

then return it with the provided download action or

def imageLookup():
    id = request.vars.id
    #response.headers['Content-Type']='image/jpeg' 
    path = os.path.join(request.folder,'uploads',db.image[id].image_file)
    return response.stream(open(path,'rb'))



On Thursday, 5 July 2012 06:23:31 UTC-5, RCTYCO wrote:
>
>
> I am trying to save an image from another database into my 
> database, essentially i am caching the a copy into my database so I don't 
> have to query the external site. 
>
> I have a link 
> http://eandata.com/image/products/004/900/000/0049000000443.jpg. 
>
> I want to save the jpg into a blob in my database.
>
> Model
> db.define_table('image',
>                 Field('url','string'),
>                 Field('image_upload','upload',uploadfield='image_file'),
>                 Field('image_file','blob'))
>
> Function. I have this running a module and called when I need to download 
> the external image.
>
> def getImage(url,db): 
>     
>     user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
>     headers = {'User-Agent': user_agent}
>     
>     req = urllib2.Request(url,"",headers)
>     
>     try:
>         response = urllib2.urlopen(req)
>     except urllib2.URLError, e:
>         if hasattr(e, 'reason'):
>             print 'We failed to reach a server.'
>             print 'Reason: ', e.reason
>         elif hasattr(e, 'code'):
>             print 'The server couldn\'t fulfill the request.'
>             print 'Error code: ', e.code
>     else:
>         pass # everything is fine
>     
>     print response.geturl()
>     print response.info()
>     print response.read()
>     f = open(response.read(),'rb')
>     #stream = open(response,'rb')
>     
>     return 
> db.image.insert(url=url,image=db.image.image_upload.store(f,url))
>
>
> *I am having a problem with my insert statement. I can't seem to figure 
> out how to save the image into database.* urllib2.urlopen(req) will 
> download the file and provide a file object. But I don't know if i can just 
> save the file or do i need to read the file object within 
> the urllib2.urlopen(req).
>
> I would like the image to be available to be via 
> http://mydomain.com/myapp/imageLookup?id=2 
> <http://mydomain.com/myapp/imageLookup?id=2>
>
> Controller
> def imageLookup():
>     id = request.vars.id
>     #response.headers['Content-Type']='image/jpeg' 
>     
>     return db.image[id].image_file
>
>
> Do you have any suggestions? The code abo
>

Reply via email to