update_media_info.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. from model.sql_models import DB
  2. from config.using_config import quchen_text, dm
  3. import pandas
  4. import requests
  5. import time
  6. def update_media_info():
  7. # 1.按用户维度获取图片
  8. sql = '''
  9. select foo.account_id ,access_token ,image_ids from
  10. (select account_id ,group_concat(image_id) as image_ids from adcreative_info ai
  11. where is_video =0 and length (account_id )>1
  12. group by account_id ) as foo
  13. left join
  14. (select account_id,access_token,'MP' as type from advertiser_vx av
  15. union
  16. select account_id ,access_token,'GDT' as type from advertiser_qq aq ) as foo2
  17. on foo.account_id=foo2.account_id
  18. order by foo.account_id
  19. limit 10
  20. '''
  21. df = pandas.read_sql(sql=sql, con=db_qc.engine)
  22. print(df)
  23. for row in df.rows():
  24. # 2.图片重复去除
  25. print(row)
  26. user_sql = '''
  27. select * from
  28. where
  29. '''
  30. #3.获取图片信息(腾讯),图片信息(大小,格式)
  31. from PIL import Image
  32. from io import BytesIO
  33. def update_image_info():
  34. while True:
  35. user_sql='''
  36. select * from image_info ii
  37. where preview_url is not null
  38. and size is null
  39. limit 100
  40. '''
  41. df=pandas.read_sql(user_sql,con=db_qc.engine)
  42. if len(df)==0:
  43. break
  44. for index,row in df.iterrows():
  45. try:
  46. print(row['preview_url'])
  47. rsp=requests.get(row['preview_url'])
  48. #1.图片写入内存
  49. im = Image.open(BytesIO(rsp.content))
  50. #2.获取图片属性
  51. image_format = im.format
  52. image_size = len(rsp.content)
  53. #3.数据进行存储
  54. sql=''' update image_info set size={},type='{}'
  55. where image_id = '{}' '''.format(image_size,image_format,row['image_id'])
  56. db_qc.session.execute(sql)
  57. db_qc.session.commit()
  58. except Exception as e:
  59. time.sleep(1)
  60. print(e)
  61. if __name__ == '__main__':
  62. db_qc = DB(config=quchen_text)
  63. db_dm = DB(config=dm)
  64. # update_media_info()
  65. update_image_info()