def _upload_preview_image(self): os.makedirs(os.path.join(os.getcwd(), "preview_images"), 0755) raw_image = os.path.join(os.getcwd(), "preview_images", self.channel + "_" + self._ip) thumbnail = os.path.join(os.getcwd(), "preview_images", self.channel + "_" + self._ip) upload_client = UploadClient( self._upload_server, self._project_name, self._ivt.id, self._ivt.login_passwd, self.channel ) while True: try: if self._is_online != self.STATE_OFFLINE: res = requests.get("http://{0}/snapshot_ch=1".format(self._ip), stream=True) res.raise_for_status() for data in res.iter_content(1024): with open(raw_image, "wb") as f: f.write(data) # TODO scale image down # upload image to IVC with open(thumbnail, "rb") as f: upload_client.upload(f) except Exception: try: res.close() except Exception: pass log.exception("Failed to fetch or upload preview image of {0}".format(self)) finally: gevent.sleep(self._upload_interval)
def _upload_preview_image(self): os.makedirs(os.path.join(os.getcwd(), 'preview_images'), 0755) raw_image = os.path.join(os.getcwd(), 'preview_images', self.channel + '_' + self._ip) thumbnail = os.path.join(os.getcwd(), 'preview_images', self.channel + '_' + self._ip) upload_client = UploadClient(self._upload_server, self._project_name, self._ivt.id, self._ivt.login_passwd, self.channel) while True: try: if self._is_online != self.STATE_OFFLINE: res = requests.get('http://{0}/snapshot_ch=1'.format( self._ip), stream=True) res.raise_for_status() for data in res.iter_content(1024): with open(raw_image, 'wb') as f: f.write(data) # TODO scale image down # upload image to IVC with open(thumbnail, 'rb') as f: upload_client.upload(f) except Exception: try: res.close() except Exception: pass log.exception( 'Failed to fetch or upload preview image of {0}'.format( self)) finally: gevent.sleep(self._upload_interval)
def _upload_preview_image(self): # wait for preivew_upload_server fetched gevent.sleep(30) try: os.makedirs(os.path.join(os.getcwd(), 'preview_images'), 0755) except Exception as e: if e.errno != errno.EEXIST: log.exception('Failed to create directory to hold temporary preview image') raise raw_image = os.path.join(os.getcwd(), 'preview_images', str(self.channel)+'_'+self._ip+'.jpg') thumbnail = os.path.join(os.getcwd(), 'preview_images', str(self.channel)+'_'+self._ip+'_thumb.jpg') upload_client = None while True: try: if not self._ivt.preview_upload_url: continue if not upload_client: upload_client = UploadClient(self._ivt.preview_upload_url, self._ivt.id, self._ivt.login_passwd, self.channel) else: upload_client.update_url(self._ivt.preview_upload_url) if self._is_online != self.STATE_OFFLINE and self._preview_url: res = requests.get(self._preview_url, stream=True, timeout=10) res.raise_for_status() with open(raw_image, 'wb') as f: for data in res.iter_content(1024): f.write(data) subprocess.check_call(['ffmpeg', '-i', raw_image, '-y', '-vf', 'scale=352:-1', thumbnail]) # upload image to IVC with open(thumbnail, 'rb') as f: upload_client.upload(f) log.info('Upload preview image successfully') except Exception: try: res.close() except Exception: pass log.exception('Failed to fetch or upload preview image of {0}'.format(self)) finally: gevent.sleep(self._upload_interval)