#!/usr/bin/env python # -*- coding: utf-8 -*- """ demeter web name:pub.py author:rabin """ from demeter.core import * import oss2 from itertools import islice from datetime import * key = 'xinnongbaohe' host = 'oss-cn-shanghai.aliyuncs.com' appId = 'LTAIsuklCbkXzpgt' appSecret = 'M4d7xLxY0bVE034rTL84MEasG8Q8YZ' url = 'http://'+key+'.'+host+'/' auth = oss2.Auth(appId, appSecret) endpoint = 'http://' + host bucket = oss2.Bucket(auth, endpoint, key) fix = '.jpg' def pic(): model = Demeter.model('device_info') model.hardware_type = 6 data = model.select() if data: for value in data: if url in value['value']: prefix = value['value'].replace(url, '') prefix = prefix.replace(fix, '') ossLoad(value, prefix) def ossLoad(value, prefix, marker=''): #data = oss2.ObjectIterator(bucket, prefix=prefix) data = bucket.list_objects(prefix=prefix, marker=marker, max_keys=100) if data.is_truncated == True: ossLoad(value, prefix, data.next_marker) elif data.object_list: pics = [] for v in data.object_list: pics.append((v.key, v.last_modified)) for v in pics[-10:]: if v[0]: savePic(value, v[0], v[1]) #oss2.resumable_download(bucket, 'remote.txt', 'local.txt') def savePic(value, file, cur): spic = url + file model = Demeter.model('device_pic') model.spic = spic data = model.select() if not data: day = str(date.today()) day = day.split('-') file_name = spic file_path = day[0] + '/' + day[1] + '/' + day[2] file_path = File.mkdirs(os.path.join(Demeter.path, 'runtime','camera', file_path)) + '/' + Demeter.md5(file_name) + '.jpg' oss2.resumable_download(bucket, file, file_path) model.pic = file_path.replace(Demeter.path + 'runtime', '') model.spic = spic model.farm_id = value['farm_id'] model.gateway_id = value['gateway_id'] model.device_id = value['id'] model.sdate = cur id = model.insert() if id: bucket.delete_object(file) #pic()