Merge branch 'wikipedia'

This commit is contained in:
matteo
2018-12-15 16:51:21 +01:00
6 changed files with 217 additions and 82 deletions
+20
View File
@@ -839,6 +839,21 @@
"to-fast-properties": "^2.0.0"
}
},
"@c0b41/ytclear": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/@c0b41/ytclear/-/ytclear-1.0.0.tgz",
"integrity": "sha512-XRlrOlcUZhFURt6x1G1q40Oq4SvYr3j0NAcZeahx4N3em8eAY8831jPjeRBT+nQd762yYINanlfUX2uaG3UaaA==",
"requires": {
"minimist": "^1.1.1"
},
"dependencies": {
"minimist": {
"version": "1.2.0",
"resolved": "http://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz",
"integrity": "sha1-o1AIsg9BOD7sH7kU9M1d95omQoQ="
}
}
},
"@csstools/convert-colors": {
"version": "1.4.0",
"resolved": "https://registry.npmjs.org/@csstools/convert-colors/-/convert-colors-1.4.0.tgz",
@@ -15009,6 +15024,11 @@
"resolved": "https://registry.npmjs.org/which-module/-/which-module-2.0.0.tgz",
"integrity": "sha1-2e8H3Od7mQK4o6j6SzHD4/fm6Ho="
},
"wikidata-sdk": {
"version": "5.15.9",
"resolved": "https://registry.npmjs.org/wikidata-sdk/-/wikidata-sdk-5.15.9.tgz",
"integrity": "sha512-I40/v7vW3gw1aLZ9/Yao4+I5ETPoOgE+3dt4rQxJkS510ow4nPb6DTtvA95zppmtDMDSNCNA1a7HN6FNRtAFQg=="
},
"wikijs": {
"version": "4.8.1",
"resolved": "https://registry.npmjs.org/wikijs/-/wikijs-4.8.1.tgz",
+2
View File
@@ -5,6 +5,7 @@
"dependencies": {
"axios": "latest",
"body-parser": "^1.18.3",
"@c0b41/ytclear": "^1.0.0",
"bootstrap": "^4.1.3",
"express": "^4.16.4",
"lodash-id": "^0.14.0",
@@ -17,6 +18,7 @@
"react-router-dom": "^4.3.1",
"react-scripts": "2.1.1",
"react-youtube": "^7.8.0",
"wikidata-sdk": "^5.15.9",
"wikijs": "^4.8.1"
},
"scripts": {
+1 -1
View File
@@ -19,7 +19,7 @@
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>react-4</title>
<title>HazeTv</title>
</head>
<body>
<noscript>
+14 -4
View File
@@ -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
View File
@@ -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
View File
@@ -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;