You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
70 lines
1.9 KiB
70 lines
1.9 KiB
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')) |
|
|
|
|
|
|