import React from 'react'; import Wikipedia from './areainfo/Wikipedia'; import { Tabs, Tab, Table } from 'react-bootstrap'; import { withRouter } from 'react-router-dom'; 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'; import './css/VideoInfo.scss'; const ytclear = require('@c0b41/ytclear'); class VideoInfo extends React.Component { state = { error: null, isLoaded: false, VideoDetails: {}, videoId: '', comments: [], others: {} }; momentDuration(duration) { return moment.duration(duration).format('hh:mm:ss'); } ISO_8601parse(a) { return moment(a, moment.ISO_8601).format('ddd, DD/MM/YYYY hh:mm:ss'); } isArtistOrTitle(a){ // switch(parseInt(b)){ // case 1: // break; // case 2: // break; // default: // break; // } axios.all([ 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([ axios.get('https://www.googleapis.com/youtube/v3/videos', { //Richiesta per tutte le info sul video 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) => { this.isArtistOrTitle(ytclear(videosResponse.data.items[0].snippet.title).split('-')); this.setState({ VideoDetails: videosResponse.data.items[0], comments: commentThreadResponse.data.items, others: { artist: null, title: videosResponse.data.items[0].snippet.title }, isLoaded: true }); }), error => { console.error(error); this.setState({ isLoaded: true, error }); }); } static getDerivedStateFromProps(nextProps, prevState) { if(nextProps.videoId !== prevState.videoId) return { videoId: nextProps.videoId }; return null; } componentDidMount() { this.getInfoComments(); } componentDidUpdate(prevProps, prevState, snapshot) { if(prevState.videoId !== this.state.videoId) this.getInfoComments(); } // shouldComponentUpdate(nextProps, nextState) { // } render() { const { error, isLoaded, VideoDetails, comments } = this.state; 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 ( {} Error: {error.message} -- Cannot get {error.config.url} ); } else if (!isLoaded) { return Loading...; } else { return ( {VideoDetails.snippet.title}

ID Video: {VideoDetails.id}
Published: {ISO_8601parse(VideoDetails.snippet.publishedAt)}
Channel name: {VideoDetails.snippet.channelTitle}
Description: {VideoDetails.snippet.description}
Tags: {VideoDetails.snippet.tags ? VideoDetails.snippet.tags.map(tag => ({tag})) : ""} {/* tags should be links */}

Duration: {momentDuration(VideoDetails.contentDetails.duration)}
Definition: {VideoDetails.contentDetails.definition}
Dimension: {VideoDetails.contentDetails.dimension}
Views: {VideoDetails.statistics.viewCount}
Likes: {VideoDetails.statistics.likeCount}{String.fromCodePoint(0x1F44D)}
Dislikes: {VideoDetails.statistics.dislikeCount}{String.fromCodePoint(0x1F44E)}

{comments.map(function (comment, index) { return ( {comment.snippet.topLevelComment.snippet.authorDisplayName} {comment.snippet.topLevelComment.snippet.textOriginal} ) })}
); } } } VideoInfo.propTypes = { }; export default withRouter(VideoInfo);