savepic.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # -*- coding: utf-8 -*-
  2. """
  3. demeter web
  4. name:admin.py
  5. author:rabin
  6. """
  7. import time
  8. from demeter.core import *
  9. import oss2
  10. from itertools import islice
  11. timeSleep = 1800
  12. key = 'xinnongbaohe'
  13. host = 'oss-cn-shanghai.aliyuncs.com'
  14. appId = 'LTAIsuklCbkXzpgt'
  15. appSecret = 'M4d7xLxY0bVE034rTL84MEasG8Q8YZ'
  16. url = 'http://'+key+'.'+host+'/'
  17. auth = oss2.Auth(appId, appSecret)
  18. endpoint = 'http://' + host
  19. bucket = oss2.Bucket(auth, endpoint, key)
  20. fix = '.jpg'
  21. # 更改控制设备的超时状态
  22. class Savepic(object):
  23. def run(self):
  24. self.handle()
  25. time.sleep(timeSleep)
  26. def handle(self):
  27. pic()
  28. def pic():
  29. model = Demeter.model('device_info')
  30. model.hardware_type = 6
  31. data = model.select()
  32. if data:
  33. for value in data:
  34. if url in value['value']:
  35. prefix = value['value'].replace(url, '')
  36. prefix = prefix.replace(fix, '')
  37. ossLoad(value, prefix)
  38. def ossLoad(value, prefix, marker=''):
  39. #data = oss2.ObjectIterator(bucket, prefix=prefix)
  40. data = bucket.list_objects(prefix=prefix, marker=marker, max_keys=100)
  41. if data.is_truncated == True:
  42. ossLoad(value, prefix, data.next_marker)
  43. elif data.object_list:
  44. pics = []
  45. for v in data.object_list:
  46. pics.append((v.key, v.last_modified))
  47. for v in pics[-10:]:
  48. savePic(value, v[0], v[1])
  49. #oss2.resumable_download(bucket, 'remote.txt', 'local.txt')
  50. def savePic(value, file, cur):
  51. spic = url + file
  52. model = Demeter.model('device_pic')
  53. model.spic = spic
  54. data = model.select()
  55. if not data:
  56. day = str(date.today())
  57. day = day.split('-')
  58. file_name = spic
  59. file_path = day[0] + '/' + day[1] + '/' + day[2]
  60. file_path = File.mkdirs(os.path.join(Demeter.path, 'runtime','camera', file_path)) + '/' + Demeter.md5(file_name) + '.jpg'
  61. oss2.resumable_download(bucket, file, file_path)
  62. model.pic = file_path.replace(Demeter.path + 'runtime', '')
  63. model.spic = spic
  64. model.farm_id = value['farm_id']
  65. model.gateway_id = value['gateway_id']
  66. model.device_id = value['id']
  67. model.sdate = cur
  68. id = model.insert()
  69. if id:
  70. bucket.delete_object(file)
  71. #pic()