PeertubeInfo/app.py

78 lines
2.3 KiB
Python
Raw Normal View History

2020-04-12 15:48:58 +00:00
from flask import Flask, jsonify, render_template, request, redirect, url_for
from flask_basicauth import BasicAuth
import constants
import requests
import json
import peertube
from peertube.rest import ApiException
app = Flask(__name__)
app.config['BASIC_AUTH_USERNAME'] = constant.AUTH_USERNAME
app.config['BASIC_AUTH_PASSWORD'] = constant.AUTH_PASSWD
app.config['BASIC_AUTH_FORCE'] = True
basic_auth = BasicAuth(app)
pt_conf = peertube.Configuration()
pt_conf.host = "https://cinematheque.tube/api/v1"
liste_videos = []
def update_video_list(token):
pt_conf.access_token = token
print('token :' + token)
with peertube.ApiClient(pt_conf) as api_client:
api_instance = peertube.MyUserApi(api_client)
try:
api_response = api_instance.users_me_videos_get(start=0,count=100, sort='-createdAt')
print('nombre de videos apres requete API: ' + str(len(api_response.data)))
return parse_video_list(api_response.data)
except ApiException as e:
print("Exception when calling MyUserApi->users_me_videos_get: %s\n" % e)
def parse_video_list(unformatted_list):
liste_videos = []
print('Nombre de videos à parser :' + str(len(unformatted_list)))
for video in unformatted_list:
liste_videos.append({
'name':video.name,
'uuid':video.uuid,
'views': video.views,
'embed_path': video.embed_path,
'thumbnail_path': video.thumbnail_path,
'created_at' : video.created_at
})
return liste_videos
def get_token():
r = requests.post('https://cinematheque.tube/api/v1/users/token',data = {
'client_id':'zw484829p5q2e1xfo84ev907qnbhjcmz',
'client_secret':'3rYtuIWCbwl3OjWdXg5V3nmY1PKBe1Rt',
'grant_type':'password',
'response_type':'code',
'username': constant.PT_USERNAME,
'password':constant.PT_PASSWD })
return json.loads(r.text)['access_token']
@app.route('/')
def index():
return render_template('index.html')
@app.route('/getVideos')
def get_videos():
global liste_videos
# if dateliste not tropjeune or not liste_videos:
if not liste_videos:
token = get_token()
liste_videos = update_video_list(token)
return(json.dumps(liste_videos))
if __name__ == '__main__':
app.run()