Archived
244 lines
7.7 KiB
JavaScript
244 lines
7.7 KiB
JavaScript
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 {
|
|
|
|
state = {
|
|
wikidata: null,
|
|
wikipedia: null,
|
|
musicbrainz: null,
|
|
props: {},
|
|
error: null,
|
|
isWikiLoaded: false,
|
|
isWDataLoaded: false,
|
|
isMBLoaded: false,
|
|
isLoaded: false,
|
|
wikidatakeys: null,
|
|
wikipediakeys: null
|
|
};
|
|
|
|
ISO_8601toYYYY(a) {
|
|
return moment(a, [moment.ISO_8601, 'YYYY']).format('YYYY');
|
|
}
|
|
|
|
filterWikipediaRes(results) {
|
|
console.log('filterWikipediaRes', results);
|
|
return results[0];
|
|
}
|
|
|
|
getWikidataPage(wikidataPXXXXResID) {
|
|
return axios.get(wdk.getEntities(wikidataPXXXXResID, 'en'))
|
|
.then(wikidataPage => {
|
|
let wikidatatmp = wikidataPage.data.entities[Object.keys(wikidataPage.data.entities)[0]];
|
|
this.setState({
|
|
wikidata: wikidatatmp,
|
|
wikidatakeys: Object.keys(wikidatatmp.claims),
|
|
isWDataLoaded: true
|
|
});
|
|
if (wikidatatmp.sitelinks.enwiki)
|
|
wikijs()
|
|
.page(wikidatatmp.sitelinks.enwiki.title)
|
|
.then(page => Promise.all([page.fullInfo(), page.summary()]))
|
|
.then(wikipediaRes => this.setState({
|
|
wikipedia: {
|
|
"desc": wikipediaRes[1],
|
|
"info": wikipediaRes[0].general
|
|
},
|
|
wikipediakeys: Object.keys(wikipediaRes[0].general),
|
|
isWikiLoaded: true
|
|
}))
|
|
else{ console.info('called here')
|
|
this.findWikiPage();}
|
|
|
|
}, error => console.error(error))
|
|
}
|
|
|
|
findWikiPage = () => {
|
|
let wikiquery = `${'undefined' === typeof this.props.title ? this.props.title2.trim() : this.props.title.trim()} (${'undefined' === typeof this.props.artist ? this.props.artist2.trim() : this.props.artist.trim()} song)`;
|
|
console.info(wikiquery)
|
|
return wikijs()
|
|
.find(wikiquery)
|
|
.then(res => {
|
|
console.info(res,'a');
|
|
return Promise.all([res.fullInfo(), res.summary()]);
|
|
}) // find by props.title (props.artist song)
|
|
.then(wikipediaRes => this.setState({
|
|
wikipedia: {
|
|
"desc": wikipediaRes[1],
|
|
"info": wikipediaRes[0].general
|
|
},
|
|
wikipediakeys: Object.keys(wikipediaRes[0].general),
|
|
isWikiLoaded: true
|
|
}))
|
|
}
|
|
|
|
wrapper() {
|
|
const musicbrainzBaseUrl = "https://musicbrainz.org/ws/2"; // more readable
|
|
return 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(() => {
|
|
if (this.state.wikidata.claims.P435) { // p435 == musicbrainzWorkId
|
|
axios.get(`${musicbrainzBaseUrl}/work/${this.state.wikidata.claims.P435[0].mainsnak.datavalue.value}`, { // get musicbrainz info
|
|
params: {
|
|
'inc': 'artist-rels url-rels',
|
|
'limit': 15,
|
|
'offset': 0,
|
|
'fmt': 'json'
|
|
}
|
|
}).then(
|
|
musicbrainzRes1 =>
|
|
this.setState({ musicbrainz: musicbrainzRes1.data, isMBLoaded: true })
|
|
);
|
|
}
|
|
else {//oof P435 not found in wikidata
|
|
axios.get(`${musicbrainzBaseUrl}/work`, { // search on mb by wikidata title
|
|
params: {
|
|
'query': `work:${this.state.wikidata.labels.en.value}`,
|
|
'limit': 1,
|
|
'offset': 0,
|
|
'fmt': 'json'
|
|
}
|
|
}).then(musicbrainzRes2 => {
|
|
axios.get(`${musicbrainzBaseUrl}/work/${musicbrainzRes2.data.works[0].id}`,
|
|
{ // get musicbrainz info on track by previous id
|
|
params: {
|
|
'inc': 'artist-rels url-rels',
|
|
'limit': 15,
|
|
'fmt': 'json'
|
|
}
|
|
})
|
|
.then(
|
|
(musicbrainzWorkRes2) =>
|
|
this.setState({ musicbrainz: musicbrainzWorkRes2.data, isMBLoaded: true })
|
|
);
|
|
});
|
|
}
|
|
});
|
|
}
|
|
else { //oof
|
|
axios.get(`${musicbrainzBaseUrl}/work`, {
|
|
params: {
|
|
'query': 'undefined' === typeof this.props.title ? this.props.title2 : this.props.title,
|
|
'limit': 1,
|
|
'offset': 0,
|
|
'fmt': 'json'
|
|
}
|
|
}).then(musicbrainzRes => {
|
|
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',
|
|
'limit': 15,
|
|
'fmt': 'json'
|
|
}
|
|
})
|
|
]).then(
|
|
axios.spread((wikidataP435Res, musicbrainzWorkRes) => {
|
|
this.setState({
|
|
musicbrainz: musicbrainzWorkRes.data,
|
|
isMBLoaded: true
|
|
});
|
|
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 wikidataUrl = 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(wikidataUrl.url.resource)) //.then(() => this.setState({ isWikiLoaded: true }));
|
|
}
|
|
else { // oof
|
|
this.findWikiPage()
|
|
}
|
|
})
|
|
)
|
|
})
|
|
}
|
|
return new Promise((resolve, reject) => resolve('ok'));
|
|
});
|
|
}
|
|
|
|
|
|
componentDidMount() {
|
|
this.wrapper().then(() => this.setState({ isLoaded: true }));
|
|
}
|
|
|
|
|
|
// shouldComponentUpdate(nextProps, nextState) {
|
|
|
|
// }
|
|
|
|
// componentDidUpdate(prevProps, prevState) {
|
|
|
|
// }
|
|
|
|
// componentWillUnmount() {
|
|
|
|
// }
|
|
|
|
render() {
|
|
const { wikidata, wikipedia, musicbrainz, error, isWikiLoaded, isWDataLoaded, isMBLoaded } = this.state;
|
|
const ISO_8601toYYYY = this.ISO_8601toYYYY;
|
|
if (error) {
|
|
return <React.Fragment>Error: {error.message}</React.Fragment>;
|
|
} else if (!this.state.isLoaded) {
|
|
return <React.Fragment>Loading...</React.Fragment>;
|
|
} else {
|
|
return (
|
|
<Table bordered variant="dark" hover size="sm">
|
|
<tbody>
|
|
{isWikiLoaded &&
|
|
this.state.wikipediakeys.map(key => (
|
|
<tr key={key}>
|
|
<td>{key}</td>
|
|
<td>{wikipedia.info[key].toLocaleString()}</td>
|
|
</tr>
|
|
))
|
|
}
|
|
{isWDataLoaded &&
|
|
this.state.wikidatakeys.map(key => (
|
|
<tr key={key}>
|
|
<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>
|
|
))
|
|
}
|
|
{isMBLoaded &&
|
|
musicbrainz.relations.map((relation, i) => {
|
|
if (relation["target-type"] === "artist")
|
|
return (<tr key={i}>
|
|
<td>{relation.type}</td>
|
|
<td>{relation.artist.name}</td>
|
|
</tr>);
|
|
else if (relation["target-type"] === "url")
|
|
return (<tr key={i}>
|
|
<td>{relation.type}</td>
|
|
<td><a href={relation.url.resource}>{relation.type}</a></td>
|
|
</tr>)
|
|
else
|
|
return ("");
|
|
})
|
|
}
|
|
</tbody>
|
|
</Table>
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
export default Wikipedia;
|