From fcfb2bb8465104850ab24609fd4472e17b8f4f86 Mon Sep 17 00:00:00 2001 From: Lasse Server Date: Thu, 16 Sep 2021 14:29:39 +0200 Subject: [PATCH] Added this to repo --- docker/profile_pictures/images.py | 70 +++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 docker/profile_pictures/images.py diff --git a/docker/profile_pictures/images.py b/docker/profile_pictures/images.py new file mode 100644 index 0000000..352f27b --- /dev/null +++ b/docker/profile_pictures/images.py @@ -0,0 +1,70 @@ +import requests +import os +from datetime import date, datetime, timedelta +from time import sleep + +from arangodb import db + + +def download_image(url, user, id): + + # Kolla så användarmappen finns + if not os.path.isdir(f'../profile_pictures/{user}'): + os.mkdir(f'../profile_pictures/{user}') + + # Ladda ner bilden + r = requests.get(url) + if r.text == 'URL signature expired': + print('För gammal länk.') + exit() + elif r.status_code == 403: + exit() + img_data = r.content + with open(f'../profile_pictures/{user}/{id}.jpg', 'wb') as handler: + handler.write(img_data) + + +def get_pictures(day): + cursor = db.aql.execute( + """ + for doc in members + filter doc.fetched == @date + filter has(doc, "checked_pictures") + filter not has(doc, "pictures_downloaded") + return {'member': doc._key, 'pictures':doc.checked_pictures} + """, + bind_vars={'date': day} + ) + + for doc in cursor: + pictures = [] + for picture in doc['pictures']: + pictures.append(picture[picture.find('fbid=')+5:]) + + + cursor = db.aql.execute( + """ + for doc in pictures + filter doc._key in @list + limit 10 + return {'_key': doc._key, 'user':doc.user, 'url': doc.src} + """, + bind_vars={"list": pictures}, + ) + + for picture in cursor: + download_image(picture['url'], picture['user'], picture['_key']) + print(picture['_key']) + sleep(2) + + db.update_document({'_id': 'members/' + str(doc['member']), 'pictures_downloaded': True}, silent=True, check_rev=False) + +def old_pics(): + if not os.path.isdir(f'../profile_pictures'): + os.mkdir(f'../profile_pictures') + start = date.today() + for i in range(1,60): + d = start - timedelta(days=i) + get_pictures(d.strftime('%Y%m%d')) + +