60 lines
2.3 KiB
Python
60 lines
2.3 KiB
Python
from django.views.generic import View
|
|
from django.http.response import HttpResponse
|
|
|
|
import shutil
|
|
import requests
|
|
import json
|
|
|
|
def save_image(post):
|
|
res = requests.get(post['media'], stream = True)
|
|
file_name = "/var/www/sciodog.fr/blog/static/blog/posts/" + post['url'].split('/')[-1]
|
|
|
|
try:
|
|
if res.status_code == 200:
|
|
with open(file_name,'wb') as f:
|
|
shutil.copyfileobj(res.raw, f)
|
|
post['media'] = "https://sciodog.fr/static/blog/posts/" + post['url'].split('/')[-1]
|
|
except IsADirectoryError as e:
|
|
pass
|
|
|
|
class IndexView(View):
|
|
template_name = "blog/index.html"
|
|
|
|
def get(self, *args, **kwargs):
|
|
context = {}
|
|
access_token = 'EAAEiPRfMZCtsBO9WSI2FMTKXeZBOFXoZBtHBbtBVLeSJh4ZAqIZC9PasW0li3IbPsWXrsVYn1yxPnRmMP8ZCs0aPT64dRd9FHaw0px6C61YwbgZALgNLCYWqD7MeTZCIq09MGwz044B2FO2JiuLMl0r16tNzHwJN4fQ5puXJeL6TZAUvcgaoIOglU1OEN55cssvYZD'
|
|
params = {
|
|
'access_token': access_token
|
|
}
|
|
url = 'https://graph.facebook.com/v13.0/102786409210660/feed?fields=attachments,message,permalink_url&limit=10'
|
|
response = requests.get(url, params=params)
|
|
posts = []
|
|
if response.status_code == 200:
|
|
data = response.json()
|
|
for post in data['data']:
|
|
ok = True
|
|
temp = {}
|
|
if 'message' in post.keys():
|
|
temp['message'] = post['message']
|
|
else:
|
|
ok = False
|
|
if 'attachments' in post.keys():
|
|
try:
|
|
try:
|
|
temp['media'] = post['attachments']['data'][0]['media']['image']['src']
|
|
except:
|
|
temp['media'] = post['attachments']['data'][0]['media']['image']['src']
|
|
except:
|
|
ok = False
|
|
else:
|
|
ok = False
|
|
if 'permalink_url' in post.keys():
|
|
temp['url'] = post['permalink_url']
|
|
else:
|
|
ok = False
|
|
if ok:
|
|
posts.append(temp)
|
|
save_image(temp)
|
|
else:
|
|
return HttpResponse(status=300, content=response.text)
|
|
return HttpResponse(status=200, content=json.dumps(posts))
|