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

205 lines
6.3 KiB
JavaScript
Raw Normal View History

2018-11-15 18:33:17 +01:00
import React, { Component } from 'react';
// import PropTypes from 'prop-types';
import wikijs from 'wikijs';
2018-12-11 03:56:13 +01:00
import wdk from 'wikidata-sdk';
import axios from 'axios';
import { Table } from 'react-bootstrap';
2018-12-15 16:48:19 +01:00
import moment from 'moment';
2018-11-15 18:33:17 +01:00
class Wikipedia extends Component {
2018-12-01 03:04:08 +01:00
constructor(props) {
super(props);
this.state = {
2018-12-11 03:56:13 +01:00
wikidata: null,
wikipedia: null,
2018-12-11 19:48:04 +01:00
musicbrainz: null,
2018-12-11 03:56:13 +01:00
spareobj: {},
2018-12-01 03:04:08 +01:00
error: null,
2018-12-15 17:32:51 +01:00
isWikiLoaded: false,
isWDataLoaded: false,
isMBLoaded: false
2018-12-01 03:04:08 +01:00
};
2018-12-15 16:48:19 +01:00
this.filterWikipediaRes = this.filterWikipediaRes.bind(this);
this.ISO_8601toYYYY = this.ISO_8601toYYYY.bind(this);
2018-12-11 19:48:04 +01:00
this.getWikidataPage = this.getWikidataPage.bind(this);
2018-12-01 03:04:08 +01:00
}
2018-11-15 18:33:17 +01:00
2018-12-15 16:48:19 +01:00
ISO_8601toYYYY(a) {
return moment(a, [moment.ISO_8601, 'YYYY']).format('YYYY');
2018-12-01 03:04:08 +01:00
}
2018-11-15 18:33:17 +01:00
2018-12-15 16:48:19 +01:00
filterWikipediaRes(results) {
console.log('filterWikipediaRes', results);
2018-12-01 03:04:08 +01:00
return results[0];
}
2018-11-25 19:45:16 +01:00
2018-12-15 16:48:19 +01:00
getWikidataPage(wikidataPXXXXResID) {
return axios.get(wdk.getEntities(wikidataPXXXXResID, 'en'))
.then(wikidataPage => {
2018-12-15 17:32:51 +01:00
let wikidatatmp = wikidataPage.data.entities[Object.keys(wikidataPage.data.entities)[0]];
2018-12-15 16:48:19 +01:00
this.setState({
2018-12-15 17:32:51 +01:00
wikidata: wikidatatmp,
isWDataLoaded: true
});
if (wikidatatmp.sitelinks.enwiki)
2018-12-15 16:48:19 +01:00
wikijs()
2018-12-15 17:32:51 +01:00
.page(wikidatatmp.sitelinks.enwiki.title)
2018-12-15 16:48:19 +01:00
.then(page => Promise.all([page.fullInfo(), page.summary()]))
.then(wikipediaRes => this.setState({
wikipedia: {
"desc": wikipediaRes[1],
"info": wikipediaRes[0].general
2018-12-15 17:32:51 +01:00
},
isWikiLoaded: true
2018-12-15 16:48:19 +01:00
}))
2018-12-15 17:32:51 +01:00
}, error => console.error(error))
// .then(() => {
// console.log(this.state.wikidata)
// .then(() => console.log(this.state.wikipedia));
// });
2018-12-15 16:48:19 +01:00
}
2018-11-15 18:33:17 +01:00
2018-12-01 03:04:08 +01:00
componentWillMount() {
2018-12-15 16:48:19 +01:00
const musicbrainzBaseUrl = "https://musicbrainz.org/ws/2"; // more readable
2018-12-11 19:48:04 +01:00
axios.get(wdk.getReverseClaims('P1651', this.props.videoId)) // P1651 is youtube_video_id property
.then(wikidataP1651Res => {
2018-12-15 16:48:19 +01:00
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'
}
2018-12-15 17:32:51 +01:00
}).then(musicbrainzRes1 => this.setState({ musicbrainz: musicbrainzRes1.data, isMBLoaded: true }));
2018-12-15 16:48:19 +01:00
});
} else { //oof
axios.get(`${musicbrainzBaseUrl}/work`, {
params: {
'query': this.props.title,
2018-12-11 19:48:04 +01:00
'limit': 1,
'offset': 0,
2018-12-15 16:48:19 +01:00
'fmt': 'json'
2018-12-11 19:48:04 +01:00
}
2018-12-15 16:48:19 +01:00
}).then(musicbrainzRes => {
2018-12-15 17:32:51 +01:00
this.setState({musicbrainz: musicbrainzRes.data.works[0], isMBLoaded: true });
2018-12-11 19:48:04 +01:00
axios.all([
axios.get(wdk.getReverseClaims('P435', musicbrainzRes.data.works[0].id)), // P435 is musicbrainz id
2018-12-15 16:48:19 +01:00
axios.get(`${musicbrainzBaseUrl}/work/${musicbrainzRes.data.works[0].id}`, { // get musicbrainz info
params: {
'inc': 'artist-rels url-rels',
'fmt': 'json'
2018-12-11 19:48:04 +01:00
}
})
]).then(
2018-12-15 16:48:19 +01:00
axios.spread((wikidataP435Res, musicbrainzWorkRes) => {
if (wikidataP435Res.data.results.bindings.length) // found a match
2018-12-11 19:48:04 +01:00
this.getWikidataPage(wdk.simplify.sparqlResults(wikidataP435Res.data)); // set the result in the state
2018-12-15 16:48:19 +01:00
else if (musicbrainzWorkRes.data.relations.find(currentRel => currentRel.type === "wikidata")) { // found wikidata on music brainz
2018-12-11 19:48:04 +01:00
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
2018-12-15 17:32:51 +01:00
this.getWikidataPage(wikidataEntityRegEx.exec(a.url.resource)) //.then(() => this.setState({ isWikiLoaded: true }));
2018-12-15 16:48:19 +01:00
}
else { // oof
wikijs().find(this.props.title).then(data => console.log('asd', data)) // find by props.title
2018-12-11 19:48:04 +01:00
}
})
)
})
2018-12-01 03:04:08 +01:00
}
2018-12-11 03:56:13 +01:00
});
2018-12-01 03:04:08 +01:00
}
2018-11-15 18:33:17 +01:00
2018-12-15 16:48:19 +01:00
componentDidMount() {
}
2018-12-01 03:04:08 +01:00
// componentWillReceiveProps(nextProps) {
2018-11-15 18:33:17 +01:00
2018-12-01 03:04:08 +01:00
// }
2018-11-15 18:33:17 +01:00
2018-12-01 03:04:08 +01:00
// shouldComponentUpdate(nextProps, nextState) {
2018-11-15 18:33:17 +01:00
2018-12-01 03:04:08 +01:00
// }
2018-11-15 18:33:17 +01:00
2018-12-01 03:04:08 +01:00
// componentWillUpdate(nextProps, nextState) {
2018-11-15 18:33:17 +01:00
2018-12-01 03:04:08 +01:00
// }
2018-11-15 18:33:17 +01:00
2018-12-01 03:04:08 +01:00
// componentDidUpdate(prevProps, prevState) {
2018-11-15 18:33:17 +01:00
2018-12-01 03:04:08 +01:00
// }
2018-11-15 18:33:17 +01:00
2018-12-01 03:04:08 +01:00
// componentWillUnmount() {
2018-11-15 18:33:17 +01:00
2018-12-01 03:04:08 +01:00
// }
2018-11-15 18:33:17 +01:00
2018-12-01 03:04:08 +01:00
render() {
2018-12-15 17:32:51 +01:00
const { wikidata, wikipedia, musicbrainz, error, isWikiLoaded, isWDataLoaded, isMBLoaded } = this.state;
2018-12-15 16:48:19 +01:00
const ISO_8601toYYYY = this.ISO_8601toYYYY;
2018-12-01 03:04:08 +01:00
if (error) {
return <React.Fragment>Error: {error.message}</React.Fragment>;
2018-12-15 17:32:51 +01:00
} else if (!isWikiLoaded || !isWDataLoaded || !isMBLoaded) {
2018-12-01 03:04:08 +01:00
return <React.Fragment>Loading...</React.Fragment>;
} else {
2018-12-15 16:48:19 +01:00
let keys = {
wikipedia: Object.keys(this.state.wikipedia.info),
wikidata: Object.keys(this.state.wikidata.claims)
}
2018-12-01 03:04:08 +01:00
return (
<Table striped bordered hover size="sm">
<tbody>
{
keys.wikipedia.map(key => (
<tr>
<td>{key}</td>
2018-12-15 16:48:19 +01:00
<td>{wikipedia.info[key].toLocaleString()}</td>
</tr>
))
}
{
keys.wikidata.map(key => (
<tr>
2018-12-15 16:48:19 +01:00
<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>
))
}
2018-12-15 16:48:19 +01:00
{
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>
2018-12-15 16:48:19 +01:00
);
2018-12-01 03:04:08 +01:00
}
}
2018-11-15 18:33:17 +01:00
}
2018-12-01 03:04:08 +01:00
export default Wikipedia;