pic.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. savePic(value, v[0], v[1])
  42. #oss2.resumable_download(bucket, 'remote.txt', 'local.txt')
  43. def savePic(value, file, cur):
  44. spic = url + file
  45. model = Demeter.model('device_pic')
  46. model.spic = spic
  47. data = model.select()
  48. if not data:
  49. day = str(date.today())
  50. day = day.split('-')
  51. file_name = spic
  52. file_path = day[0] + '/' + day[1] + '/' + day[2]
  53. file_path = File.mkdirs(os.path.join(Demeter.path, 'runtime','camera', file_path)) + '/' + Demeter.md5(file_name) + '.jpg'
  54. oss2.resumable_download(bucket, file, file_path)
  55. model.pic = file_path.replace(Demeter.path + 'runtime', '')
  56. model.spic = spic
  57. model.farm_id = value['farm_id']
  58. model.gateway_id = value['gateway_id']
  59. model.device_id = value['id']
  60. model.sdate = cur
  61. id = model.insert()
  62. if id:
  63. bucket.delete_object(file)
  64. #pic()