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/VideoInfo.js
T

219 lines
8.9 KiB
JavaScript
Raw Normal View History

2019-01-14 19:23:29 +01:00
import React from 'react';
2018-12-14 16:55:20 +01:00
import Wikipedia from './areainfo/Wikipedia';
2018-12-16 02:16:01 +01:00
import { Tabs, Tab, Table } from 'react-bootstrap';
2019-01-14 19:23:29 +01:00
import { withRouter } from 'react-router-dom';
2018-12-14 16:55:20 +01:00
import axios from 'axios';
import Linkify from "react-linkify";
import Card from "react-bootstrap/lib/Card";
import moment from 'moment';
import momentDurationFormat from 'moment-duration-format';
2018-12-15 22:20:26 +01:00
import './css/VideoInfo.scss';
2019-01-14 19:23:29 +01:00
const ytclear = require('@c0b41/ytclear');
2018-11-08 18:22:06 +01:00
2019-01-14 19:23:29 +01:00
class VideoInfo extends React.Component {
state = {
error: null,
isLoaded: false,
VideoDetails: {},
videoId: '',
comments: [],
others: {}
};
2018-11-08 18:22:06 +01:00
2018-12-16 02:16:01 +01:00
momentDuration(duration) {
return moment.duration(duration).format('hh:mm:ss');
2018-12-16 02:16:01 +01:00
}
2019-01-14 19:23:29 +01:00
2018-12-16 02:16:01 +01:00
ISO_8601parse(a) {
return moment(a, moment.ISO_8601).format('ddd, DD/MM/YYYY hh:mm:ss');
}
2018-11-08 18:22:06 +01:00
2019-01-14 19:23:29 +01:00
isArtistOrTitle(a){
// switch(parseInt(b)){
// case 1:
// break;
// case 2:
2018-11-08 18:22:06 +01:00
2019-01-14 19:23:29 +01:00
// break;
// default:
// break;
// }
axios.all([
2019-01-14 19:23:29 +01:00
axios.get("https://musicbrainz.org/ws/2/work", {
params: {
query: `work:${a[0]} AND artist:${a[1]}`,
//artist: a[1],
fmt: "json",
limit: 1,
inc: "aliases"
}}),
axios.get("https://musicbrainz.org/ws/2/work", {
params: {
query: `work:${a[1]} AND artist:${a[0]}`,
// artist: a[0],
fmt: "json",
limit: 1,
inc: "aliases"
}})
])
.then(axios.spread((response1, response2) => {
console.log(response1.data, response2.data);
axios.all([
axios.get(`https://musicbrainz.org/ws/2/work/${response1.data.works[0].id}`,{
params:{
fmt:'json',
limit:1,
inc: 'aliases'
}
}),
axios.get(`https://musicbrainz.org/ws/2/work/${response2.data.works[0].id}`,{
params:{
fmt:'json',
limit:1,
inc: 'recording-rels'
}
}),
]).then(axios.spread((responseA1,responseA2)=>{
console.log(responseA1.data, responseA2.data);
}))
}),
error => { }
);
}
getInfoComments() {
return axios.all([
2018-12-14 17:48:29 +01:00
axios.get('https://www.googleapis.com/youtube/v3/videos', { //Richiesta per tutte le info sul video
2018-12-16 02:16:01 +01:00
params: {
'part': 'snippet,contentDetails,statistics,topicDetails',
'id': this.state.videoId,
'key': 'AIzaSyA21Odw9UdfiOJnxvA_eDfOW0OxuUfHKig'
}
}), axios.get('https://www.googleapis.com/youtube/v3/commentThreads', { //Richiesta per ottenere i commenti del video
params: {
'part': 'snippet',
'videoId': this.state.videoId,
'order': 'relevance',
'key': 'AIzaSyA21Odw9UdfiOJnxvA_eDfOW0OxuUfHKig'
}
})])
.then(axios.spread((videosResponse, commentThreadResponse) => {
2019-01-14 19:23:29 +01:00
this.isArtistOrTitle(ytclear(videosResponse.data.items[0].snippet.title).split('-'));
this.setState({
2018-12-15 22:20:26 +01:00
VideoDetails: videosResponse.data.items[0],
comments: commentThreadResponse.data.items,
2019-01-14 19:23:29 +01:00
others: {
artist: null,
title: videosResponse.data.items[0].snippet.title
},
isLoaded: true
});
}), error => {
console.error(error);
this.setState({
isLoaded: true,
error
});
});
2018-11-25 19:45:16 +01:00
}
2018-12-14 16:55:20 +01:00
2019-01-14 19:23:29 +01:00
static getDerivedStateFromProps(nextProps, prevState) {
if(nextProps.videoId !== prevState.videoId)
return { videoId: nextProps.videoId };
return null;
}
componentDidMount() {
this.getInfoComments();
}
2018-12-15 00:24:41 +01:00
componentDidUpdate(prevProps, prevState, snapshot) {
2019-01-14 19:23:29 +01:00
if(prevState.videoId !== this.state.videoId)
this.getInfoComments();
2018-12-27 19:26:46 +01:00
}
2018-12-15 00:24:41 +01:00
2019-01-14 19:23:29 +01:00
// shouldComponentUpdate(nextProps, nextState) {
// }
2018-11-25 19:45:16 +01:00
render() {
const { error, isLoaded, VideoDetails, comments } = this.state;
2018-12-16 02:16:01 +01:00
const momentDuration = this.momentDuration; // bind momentDuration in render to the actual method
const ISO_8601parse = this.ISO_8601parse; // bind momentDuration in render to the actual method
if (error) {
return (
2018-12-27 19:26:46 +01:00
<span className="text-white">
2019-01-14 19:23:29 +01:00
<span>{}</span>
Error: {error.message} -- Cannot get {error.config.url}
2018-12-27 19:26:46 +01:00
</span>
);
} else if (!isLoaded) {
2018-12-27 19:26:46 +01:00
return <span className="text-white">Loading...</span>;
} else {
return (
<React.Fragment>
2018-12-16 02:16:01 +01:00
<span class="h5 text-white">{VideoDetails.snippet.title}</span>
<Tabs defaultActiveKey="video" id="tabs">
2018-12-27 19:26:46 +01:00
<Tab className="text-white" eventKey="video" title="Video">
2018-12-16 02:16:01 +01:00
<p>
<b>ID Video: </b>{VideoDetails.id}<br />
<b>Published: </b>{ISO_8601parse(VideoDetails.snippet.publishedAt)}<br />
<b>Channel name: </b>{VideoDetails.snippet.channelTitle}<br />
2018-12-27 19:26:46 +01:00
<b>Description: </b><Linkify>{VideoDetails.snippet.description}</Linkify><br />
2018-12-27 19:33:50 +01:00
<b>Tags: </b>{VideoDetails.snippet.tags ?
2019-01-14 19:23:29 +01:00
VideoDetails.snippet.tags.map(tag => (<span className='d-block'>{tag}</span>)) : ""}
2018-12-27 19:33:50 +01:00
{/* tags should be links */}
2018-12-16 02:16:01 +01:00
</p>
</Tab>
<Tab className="text-white" eventKey="techinfo" title="Tecnical Informations">
<p><b>Duration: </b>{momentDuration(VideoDetails.contentDetails.duration)}<br />
<b>Definition: </b> {VideoDetails.contentDetails.definition}<br />
<b>Dimension: </b> {VideoDetails.contentDetails.dimension}<br />
<b>Views: </b> {VideoDetails.statistics.viewCount}<br />
2018-12-27 19:26:46 +01:00
<b>Likes: </b> {VideoDetails.statistics.likeCount}{String.fromCodePoint(0x1F44D)}<br />
<b>Dislikes: </b> {VideoDetails.statistics.dislikeCount}{String.fromCodePoint(0x1F44E)}
2018-12-16 02:16:01 +01:00
</p>
</Tab>
<Tab eventKey="comments" title="Comment">
{comments.map(function (comment, index) {
return (
2018-12-27 19:33:50 +01:00
<Card key={index} className="my-1">
2018-12-14 16:55:20 +01:00
<Card.Body>
2018-12-27 19:33:50 +01:00
<Card.Title>
<Card.Img src={comment.snippet.topLevelComment.snippet.authorProfileImageUrl} style={{ width: '3rem', height: '3rem' }} />
{comment.snippet.topLevelComment.snippet.authorDisplayName}
</Card.Title>
2018-12-14 16:55:20 +01:00
<Card.Text>
<Linkify>
2018-12-27 19:33:50 +01:00
{comment.snippet.topLevelComment.snippet.textOriginal}
2018-12-14 16:55:20 +01:00
</Linkify>
</Card.Text>
</Card.Body>
</Card>
2018-12-16 02:16:01 +01:00
)
})}
</Tab>
2019-01-14 19:23:29 +01:00
<Tab className="text-white" eventKey="wikipedia" title="Wikipedia">
<Wikipedia
title={this.state.others.title}
artist={this.state.others.artist}
videoId={this.state.videoId}
/>
2018-12-16 02:16:01 +01:00
</Tab>
</Tabs>
</React.Fragment>
);
}
2018-11-25 19:45:16 +01:00
}
2018-11-08 18:22:06 +01:00
2018-12-27 19:33:50 +01:00
}
2018-12-14 16:55:20 +01:00
VideoInfo.propTypes = {
};
2019-01-14 19:23:29 +01:00
export default withRouter(VideoInfo);