Archived
Merge branch 'wikipedia'
This commit is contained in:
+14
-4
@@ -178,9 +178,19 @@ class VideoInfo extends Component {
|
||||
</ul>
|
||||
</Tab>
|
||||
<Tab eventKey={4} title="Wikipedia">
|
||||
<div>
|
||||
|
||||
</div>
|
||||
<Wikipedia
|
||||
title={
|
||||
this.props.location.state
|
||||
? this.props.location.state.title
|
||||
: this.state.others.title
|
||||
}
|
||||
artist={
|
||||
this.props.location.state
|
||||
? this.props.location.state.artist
|
||||
: this.state.others.artist
|
||||
}
|
||||
videoId={this.props.match.params.id}
|
||||
/>
|
||||
</Tab>
|
||||
</Tabs>
|
||||
</Col>
|
||||
@@ -197,4 +207,4 @@ VideoInfo.propTypes = {
|
||||
|
||||
};
|
||||
|
||||
export default VideoInfo;
|
||||
export default VideoInfo;
|
||||
|
||||
+5
-6
@@ -125,12 +125,11 @@ class VideoList extends React.Component {
|
||||
{items.map(item => (
|
||||
<Col key={item.videoID} lg={{ span: 3 }} md="6">
|
||||
<Card className="my-1 carta">
|
||||
<Link
|
||||
to={{
|
||||
pathname: '/video/' + item.videoID,
|
||||
state: {
|
||||
artist: item.artist,
|
||||
title: item.title
|
||||
<Link to={{
|
||||
pathname: '/video/' + item.videoID,
|
||||
state: {
|
||||
artist: item.artist,
|
||||
title: item.title
|
||||
}
|
||||
}}
|
||||
>
|
||||
|
||||
+175
-71
@@ -1,94 +1,198 @@
|
||||
import React, { Component } from 'react';
|
||||
// import PropTypes from 'prop-types';
|
||||
import wikijs from 'wikijs';
|
||||
import wdk from 'wikidata-sdk';
|
||||
import axios from 'axios';
|
||||
import { Table } from 'react-bootstrap';
|
||||
import moment from 'moment';
|
||||
|
||||
class Wikipedia extends Component {
|
||||
constructor(props) {
|
||||
super(props);
|
||||
this.state = {
|
||||
data: {},
|
||||
infobox: {},
|
||||
error: null,
|
||||
isLoaded: false
|
||||
};
|
||||
this.getWikiBox = this.getWikiBox.bind(this);
|
||||
this.raffina = this.raffina.bind(this);
|
||||
}
|
||||
constructor(props) {
|
||||
super(props);
|
||||
this.state = {
|
||||
wikidata: null,
|
||||
wikipedia: null,
|
||||
musicbrainz: null,
|
||||
spareobj: {},
|
||||
error: null,
|
||||
isLoaded: false
|
||||
};
|
||||
this.filterWikipediaRes = this.filterWikipediaRes.bind(this);
|
||||
this.ISO_8601toYYYY = this.ISO_8601toYYYY.bind(this);
|
||||
this.getWikidataPage = this.getWikidataPage.bind(this);
|
||||
}
|
||||
|
||||
getWikiBox(name){
|
||||
wikijs()
|
||||
.page(name)
|
||||
.then(page => page.fullInfo())
|
||||
.then(infobox => console.log('infobox', infobox)
|
||||
//this.setState({ infobox })
|
||||
);
|
||||
//console.log('infobox', this.state.infobox)
|
||||
}
|
||||
ISO_8601toYYYY(a) {
|
||||
return moment(a, [moment.ISO_8601, 'YYYY']).format('YYYY');
|
||||
}
|
||||
|
||||
raffina(results){
|
||||
console.log('raffina',results);
|
||||
return results[0];
|
||||
}
|
||||
filterWikipediaRes(results) {
|
||||
console.log('filterWikipediaRes', results);
|
||||
return results[0];
|
||||
}
|
||||
|
||||
// componentWillMount() {
|
||||
getWikidataPage(wikidataPXXXXResID) {
|
||||
return axios.get(wdk.getEntities(wikidataPXXXXResID, 'en'))
|
||||
.then(wikidataPage => {
|
||||
this.setState({
|
||||
wikidata: wikidataPage.data.entities[Object.keys(wikidataPage.data.entities)[0]]
|
||||
})
|
||||
}, error => console.error(error))
|
||||
.finally(() => {
|
||||
console.log(this.state.wikidata)
|
||||
if (this.state.wikidata.sitelinks.enwiki)
|
||||
wikijs()
|
||||
.page(this.state.wikidata.sitelinks.enwiki.title)
|
||||
.then(page => Promise.all([page.fullInfo(), page.summary()]))
|
||||
.then(wikipediaRes => this.setState({
|
||||
wikipedia: {
|
||||
"desc": wikipediaRes[1],
|
||||
"info": wikipediaRes[0].general
|
||||
}
|
||||
}))
|
||||
.finally(() => console.log(this.state.wikipedia));
|
||||
});
|
||||
}
|
||||
|
||||
// }
|
||||
componentWillMount() {
|
||||
const musicbrainzBaseUrl = "https://musicbrainz.org/ws/2"; // more readable
|
||||
axios.get(wdk.getReverseClaims('P1651', this.props.videoId)) // P1651 is youtube_video_id property
|
||||
.then(wikidataP1651Res => {
|
||||
if (wikidataP1651Res.data.results.bindings.length) { // got a match
|
||||
this.getWikidataPage(wdk.simplify.sparqlResults(wikidataP1651Res.data))// set in state wikidata
|
||||
.then(() => {
|
||||
console.log(this.state.wikidata.claims.P435[0].mainsnak.datavalue.value)
|
||||
axios.get(`${musicbrainzBaseUrl}/work/${this.state.wikidata.claims.P435[0].mainsnak.datavalue.value}`, { // get musicbrainz info
|
||||
params: {
|
||||
'inc': 'artist-rels url-rels',
|
||||
'limit': 1,
|
||||
'offset': 0,
|
||||
'fmt': 'json'
|
||||
}
|
||||
}).then(musicbrainzRes1 => this.setState({ isLoaded: true, musicbrainz: musicbrainzRes1.data }));
|
||||
});
|
||||
|
||||
componentWillMount() {
|
||||
wikijs().find(this.props.query, this.raffina).then(data => {
|
||||
this.setState({
|
||||
data,
|
||||
isLoaded: true
|
||||
});
|
||||
console.log(data.html());
|
||||
//this.getWikiBox(data.results[0]);
|
||||
}, error => {
|
||||
console.error(error);
|
||||
this.setState({
|
||||
isLoaded: true,
|
||||
error
|
||||
});
|
||||
});
|
||||
}
|
||||
} else { //oof
|
||||
axios.get(`${musicbrainzBaseUrl}/work`, {
|
||||
params: {
|
||||
'query': this.props.title,
|
||||
'limit': 1,
|
||||
'offset': 0,
|
||||
'fmt': 'json'
|
||||
}
|
||||
}).then(musicbrainzRes => {
|
||||
this.setState({ musicbrainz: musicbrainzRes.data.works[0] });
|
||||
axios.all([
|
||||
axios.get(wdk.getReverseClaims('P435', musicbrainzRes.data.works[0].id)), // P435 is musicbrainz id
|
||||
axios.get(`${musicbrainzBaseUrl}/work/${musicbrainzRes.data.works[0].id}`, { // get musicbrainz info
|
||||
params: {
|
||||
'inc': 'artist-rels url-rels',
|
||||
'fmt': 'json'
|
||||
}
|
||||
})
|
||||
]).then(
|
||||
axios.spread((wikidataP435Res, musicbrainzWorkRes) => {
|
||||
if (wikidataP435Res.data.results.bindings.length) // found a match
|
||||
this.getWikidataPage(wdk.simplify.sparqlResults(wikidataP435Res.data)); // set the result in the state
|
||||
else if (musicbrainzWorkRes.data.relations.find(currentRel => currentRel.type === "wikidata")) { // found wikidata on music brainz
|
||||
let a = musicbrainzWorkRes.data.relations.find(currentRel => currentRel.type === "wikidata");
|
||||
let wikidataEntityRegEx = new RegExp("[^/]+$"); // match last part of url https://regex101.com/r/0wCQHY/1
|
||||
this.getWikidataPage(wikidataEntityRegEx.exec(a.url.resource)).then(() => this.setState({ isLoaded: true }));
|
||||
}
|
||||
else { // oof
|
||||
wikijs().find(this.props.title).then(data => console.log('asd', data)) // find by props.title
|
||||
}
|
||||
})
|
||||
)
|
||||
})
|
||||
}
|
||||
});
|
||||
|
||||
// componentWillReceiveProps(nextProps) {
|
||||
}
|
||||
|
||||
// }
|
||||
componentDidMount() {
|
||||
}
|
||||
|
||||
// shouldComponentUpdate(nextProps, nextState) {
|
||||
// componentWillReceiveProps(nextProps) {
|
||||
|
||||
// }
|
||||
// }
|
||||
|
||||
// componentWillUpdate(nextProps, nextState) {
|
||||
// shouldComponentUpdate(nextProps, nextState) {
|
||||
|
||||
// }
|
||||
// }
|
||||
|
||||
// componentDidUpdate(prevProps, prevState) {
|
||||
// componentWillUpdate(nextProps, nextState) {
|
||||
|
||||
// }
|
||||
// }
|
||||
|
||||
// componentWillUnmount() {
|
||||
// componentDidUpdate(prevProps, prevState) {
|
||||
|
||||
// }
|
||||
// }
|
||||
|
||||
render() {
|
||||
const { data, error, isLoaded } = this.state;
|
||||
if (error) {
|
||||
return (
|
||||
<React.Fragment>
|
||||
Error: {error.message} -- Cannot get {error.config.url}
|
||||
</React.Fragment>
|
||||
);
|
||||
} else if (!isLoaded) {
|
||||
return <React.Fragment>Loading...</React.Fragment>;
|
||||
} else {
|
||||
return (
|
||||
<div>
|
||||
{/* <p>risultato ricerca wikipedia: { data.results[0] }</p> */}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
}
|
||||
// componentWillUnmount() {
|
||||
|
||||
// }
|
||||
|
||||
render() {
|
||||
const { wikidata, wikipedia, musicbrainz, error, isLoaded } = this.state;
|
||||
const ISO_8601toYYYY = this.ISO_8601toYYYY;
|
||||
if (error) {
|
||||
return <React.Fragment>Error: {error.message}</React.Fragment>;
|
||||
} else if (!isLoaded) {
|
||||
return <React.Fragment>Loading...</React.Fragment>;
|
||||
} else {
|
||||
let keys = {
|
||||
wikipedia: Object.keys(this.state.wikipedia.info),
|
||||
wikidata: Object.keys(this.state.wikidata.claims)
|
||||
}
|
||||
return (
|
||||
<Table striped bordered hover size="sm">
|
||||
<tbody>
|
||||
{
|
||||
keys.wikipedia.map(key => (
|
||||
<tr>
|
||||
<td>{key}</td>
|
||||
<td>{wikipedia.info[key].toLocaleString()}</td>
|
||||
</tr>
|
||||
))
|
||||
}
|
||||
{
|
||||
keys.wikidata.map(key => (
|
||||
<tr>
|
||||
<td>{`${key} - ${wikidata.claims[key][0].mainsnak.datatype}`}</td>
|
||||
<td>{
|
||||
typeof wikidata.claims[key][0].mainsnak.datavalue.value === "string" ?
|
||||
wikidata.claims[key][0].mainsnak.datavalue.value :
|
||||
typeof wikidata.claims[key][0].mainsnak.datavalue.value.id === "string" ?
|
||||
wikidata.claims[key][0].mainsnak.datavalue.value.id :
|
||||
typeof wikidata.claims[key][0].mainsnak.datavalue.value.time === "string" ?
|
||||
ISO_8601toYYYY(wikidata.claims[key][0].mainsnak.datavalue.value.time.toString()) :
|
||||
""
|
||||
}</td>
|
||||
</tr>
|
||||
))
|
||||
}
|
||||
{
|
||||
musicbrainz.relations.map(relation => {
|
||||
if (relation["target-type"] === "artist")
|
||||
return (<tr>
|
||||
<td>{relation.type}</td>
|
||||
<td>{relation.artist.name}</td>
|
||||
</tr>);
|
||||
else if (relation["target-type"] === "url")
|
||||
return (<tr>
|
||||
<td>{relation.type}</td>
|
||||
<td><a href={relation.url.resource}>{relation.type}</a></td>
|
||||
</tr>)
|
||||
else
|
||||
return ("");
|
||||
})
|
||||
}
|
||||
</tbody>
|
||||
</Table>
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default Wikipedia;
|
||||
export default Wikipedia;
|
||||
|
||||
Reference in New Issue
Block a user