pic.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. demeter web
  5. name:pub.py
  6. author:rabin
  7. """
  8. from demeter.core import *
  9. import oss2
  10. from itertools import islice
  11. from datetime import *
  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. def pic():
  22. model = Demeter.model('device_info')
  23. model.hardware_type = 6
  24. data = model.select()
  25. if data:
  26. for value in data:
  27. if url in value['value']:
  28. prefix = value['value'].replace(url, '')
  29. prefix = prefix.replace(fix, '')
  30. ossLoad(value, prefix)
  31. def ossLoad(value, prefix, marker=''):
  32. #data = oss2.ObjectIterator(bucket, prefix=prefix)
  33. data = bucket.list_objects(prefix=prefix, marker=marker, max_keys=100)
  34. if data.is_truncated == True:
  35. ossLoad(value, prefix, data.next_marker)
  36. elif data.object_list:
  37. pics = []
  38. for v in data.object_list:
  39. pics.append((v.key, v.last_modified))
  40. for v in pics[-10:]:
  41. if v[0]:
  42. savePic(value, v[0], v[1])
  43. #oss2.resumable_download(bucket, 'remote.txt', 'local.txt')
  44. def savePic(value, file, cur):
  45. spic = url + file
  46. model = Demeter.model('device_pic')
  47. model.spic = spic
  48. data = model.select()
  49. if not data:
  50. day = str(date.today())
  51. day = day.split('-')
  52. file_name = spic
  53. file_path = day[0] + '/' + day[1] + '/' + day[2]
  54. file_path = File.mkdirs(os.path.join(Demeter.path, 'runtime','camera', file_path)) + '/' + Demeter.md5(file_name) + '.jpg'
  55. oss2.resumable_download(bucket, file, file_path)
  56. model.pic = file_path.replace(Demeter.path + 'runtime', '')
  57. model.spic = spic
  58. model.farm_id = value['farm_id']
  59. model.gateway_id = value['gateway_id']
  60. model.device_id = value['id']
  61. model.sdate = cur
  62. id = model.insert()
  63. if id:
  64. bucket.delete_object(file)
  65. #pic()