This repository has been archived on 2019-10-19. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
tecnologiabanana/src/reccomenders/Random.js
T

77 lines
3.3 KiB
JavaScript
Raw Normal View History

2018-11-08 18:22:06 +01:00
import React, { Component } from 'react';
2018-12-04 17:31:52 +01:00
import 'bootstrap/dist/css/bootstrap.css';
import axios from "axios";
import createpageToken from 'youtube-page-token';
2018-12-14 18:21:55 +01:00
import {Card} from "react-bootstrap";
import {Link} from "react-router-dom";
2018-11-08 18:22:06 +01:00
2018-12-04 17:31:52 +01:00
class RecommenderRandom extends Component{
constructor(props){
super(props);
this.state={
items : [],
};
}
2018-11-08 18:22:06 +01:00
2018-12-04 17:31:52 +01:00
componentDidMount(){
let pos = Math.floor(Math.random() * 500);
let pageToken = createpageToken(pos); /* createPageToken(pos) restituisce il pageToken relativo al numero
2019-01-20 18:45:00 +01:00
di pagina che gli viene passato(in questo caso pos) che verrai poi usato
come paramentro da axios per ottenere una determinata pagina di risultati */
2018-11-08 18:22:06 +01:00
2018-12-04 17:31:52 +01:00
axios.get("https://www.googleapis.com/youtube/v3/search", {
params: {
'part': 'snippet',
'topicId' : '/m/04rlf', //Filtro per la musica.In questo caso il valore è quello della parent directory
'type' : 'video', //che contiene tutti i generi di musica
2018-12-14 18:21:55 +01:00
'maxResults': '21',
2018-12-04 17:31:52 +01:00
'pageToken' : pageToken,
2019-01-20 18:45:00 +01:00
'key': 'AIzaSyA21Odw9UdfiOJnxvA_eDfOW0OxuUfHKig' // process.env.APIKEY in production
2018-12-04 17:31:52 +01:00
}
}).then(
response => {
this.setState({
items : response.data.items,
})
},
error => {
console.error(error);
}
)
}
2018-11-08 18:22:06 +01:00
2018-12-04 17:31:52 +01:00
render(){
return (
2018-12-14 18:21:55 +01:00
<div style={{width: '100rem',height:'100rem'}}>
2018-12-04 17:31:52 +01:00
{this.state.items.map(function (item,index) {
return(
2018-12-14 18:21:55 +01:00
<div key={index} style={{margin: '1rem',float: 'left'}}>
<Card>
<Link
to={{
pathname: '/video/' + item.id.videoId,
}}
>
<Card.Img
src={item.snippet.thumbnails.high.url}
alt={'Thumbnail of ' + item.title}
style={{width:'30rem',height:'20rem'}}
/>
<Card.ImgOverlay>
<Card.Title className="bg-dark d-inline text-white">
{item.snippet.title}<br/>
{item.snippet.channelTitle} - {item.snippet.publishedAt.replace(/(\d+)-(\d+)-(\d+)T(\d+):(\d+):(\d+).000Z/,function(match,p1,p2,p3,p4,p5,p6){
return(p3+'/'+p2+'/'+p1+' at '+p4+':'+p5+':'+p6); //Funzione che trasforma la data e ora di pubblicazione dal formato ISO nel normale formato da cristiani
})}
</Card.Title>
</Card.ImgOverlay>
</Link>
</Card>
</div>
2018-12-04 17:31:52 +01:00
)
})}
</div>
);
}
2018-11-08 18:22:06 +01:00
}
2018-12-04 17:31:52 +01:00
export default RecommenderRandom;