update_media_info.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. db_qc = DB(config=quchen_text)
  35. db_dm = DB(config=dm)
  36. while True:
  37. user_sql = '''
  38. select * from image_info ii
  39. where preview_url is not null
  40. and (size is null or type is null)
  41. limit 100
  42. '''
  43. df = pandas.read_sql(user_sql, con=db_qc.engine)
  44. if len(df) == 0:
  45. break
  46. for index, row in df.iterrows():
  47. try:
  48. print(row['preview_url'])
  49. rsp = requests.get(row['preview_url'])
  50. # 1.图片写入内存
  51. im = Image.open(BytesIO(rsp.content))
  52. # 2.获取图片属性
  53. image_format = im.format
  54. image_size = len(rsp.content)
  55. # 3.数据进行存储
  56. sql = ''' update image_info set size={},type='{}'
  57. where image_id = '{}' '''.format(image_size, image_format, row['image_id'])
  58. db_qc.session.execute(sql)
  59. db_qc.session.commit()
  60. except Exception as e:
  61. time.sleep(1)
  62. print(e)
  63. if __name__ == '__main__':
  64. # update_media_info()
  65. import threading
  66. for i in range(5):
  67. thread_update = threading.Thread(target=update_image_info, )
  68. thread_update.start()