This commit is contained in:
matteo
2019-03-02 17:42:23 +01:00
parent 10d54b51e6
commit 815a713412
3 changed files with 202 additions and 106 deletions
+42 -42
View File
@@ -19,8 +19,8 @@ class VideoPlayer extends React.Component {
origin//: 'http://site1854.tw.cs.unibo.it'
}
},
promise1: null,
promise2: null,
promise1: Promise,
promise2: Promise,
outsideReject1: null,
outsideReject2: null
};
@@ -65,7 +65,7 @@ class VideoPlayer extends React.Component {
default:
break;
}
}, 500);
}, 1500);
this.setState({ outsideReject2: reject });
})
});
@@ -77,49 +77,49 @@ class VideoPlayer extends React.Component {
promise1: new Promise((resolve, reject) => {
timerId1 = window.setInterval(() => {
if (event.target.getCurrentTime() > 15)
resolve(event.target.j.videoData.video_id);
}, 1000);
resolve(event.target.getVideoData().video_id);
}, 1200);
this.setState({ outsideReject1: reject });
})
});
this.state.promise1
.then(
videoIdWatched => {
// post request to local db
axios
.post('http://site1854.tw.cs.unibo.it/videotrack', {
id: videoIdWatched.toString()
})
.then(
response => console.info(response.data),
error => this.setState({error})//console.error(error, videoIdWatched.toString())
);
// local storage logic for recent reccomender//tmp è il video che devo inserire sulla lista
let tmp = JSON.parse(localStorage.getItem('lastWatched'))
let j=tmp.findIndex(el=>el.id.toString()===videoIdWatched.toString())
if (j<0){
tmp.push({
'id': videoIdWatched.toString(),
'lastWatched' : new Date(),
'timesWatched' : 1
});}
else {
tmp[j].timesWatched++;
tmp[j].lastWatched= new Date();
}
tmp.sort((a, b) => new Date(b.lastWatched).getTime() - new Date(a.lastWatched).getTime());
localStorage.setItem('lastWatched', JSON.stringify(tmp));
//.finally(()=>{})
clearInterval(timerId1);
},
error => console.error(error)
)
.finally(() => this.setState({ promise1: null, outsideReject1: null }));
.then(
videoIdWatched => {
// post request to local db
let reason = new URLSearchParams(this.props.location.search).get("ref");
axios
.post('http://site1854.tw.cs.unibo.it/videotrack', {
id: videoIdWatched.toString(),
reason: reason || undefined
})
.then(
response => console.info(response.data),
error => this.setState({error})//console.error(error, videoIdWatched.toString())
);
// local storage logic for recent reccomender//tmp è il video che devo inserire sulla lista
let tmp = JSON.parse(localStorage.getItem('lastWatched'))
let j=tmp.findIndex(el=>el.id.toString()===videoIdWatched.toString())
if (j<0){
tmp.push({
'id': videoIdWatched.toString(),
'lastWatched' : new Date(),
'timesWatched' : 1
});}
else {
tmp[j].timesWatched++;
tmp[j].lastWatched= new Date();
}
tmp.sort((a, b) => new Date(b.lastWatched).getTime() - new Date(a.lastWatched).getTime());
localStorage.setItem('lastWatched', JSON.stringify(tmp));
//.finally(()=>{})
clearInterval(timerId1);
return new Promise ((resolve,reject) => resolve(videoIdWatched))
},
error => console.error(error)
)
.finally((id) => this.setState({ promise1: null, outsideReject1: null }));
break;
case 5:
if (this.state.promise1)
+133 -64
View File
@@ -38,13 +38,73 @@ class Wikipedia extends Component {
case "P2624": // metrolyrics id
return (<a target="_blank" rel="noopener noreferrer" href={`http://www.metrolyrics.com/${value}`}>{`${value}`}</a>)
case "P1651": // youtubeid
return ( <Link to={{pathname:`/video/${value}`}}>{`${value}`}</Link>)
return (<Link to={{ pathname: `/video/${value}` }}>{`${value}`}</Link>)
case "P2581":
return (<a target="_blank" rel="noopener noreferrer" href={`https://babelnet.org/synset?word=bn:${value}`}>{`${value}`}</a>)
case "P1827":
return (<a target="_blank" rel="noopener noreferrer" href={`https://musicbrainz.org/iswc/${value}`}>{`${value}`}</a>)
return (<a target="_blank" rel="noopener noreferrer" href={`https://babelnet.org/synset?word=bn:${value}`}>{value}</a>)
case "P1827":
return (<a target="_blank" rel="noopener noreferrer" href={`https://musicbrainz.org/iswc/${value}`}>{value}</a>)
default:
return(`${value}`)
return (`${value}`);
}
}
// P175 = artist
// P361 = album
getMusicBrainz = (P175, P361, ) => {
const musicbrainzBaseUrl = "https://musicbrainz.org/ws/2"; // more readable
let artist = this.props.artist || this.props.artist2;
let title = this.props.title || this.props.title2;
console.log('porcodio', P175.toLowerCase(), artist.trim().toLowerCase(), P175.toLowerCase() === artist.trim().toLowerCase())
if (P175.toLowerCase() === artist.trim().toLowerCase()) {
axios
.get(`https://musicbrainz.org/ws/2/recording`, {
params: {
'query': `${title.trim()} AND artistname:${artist.trim()} AND release:${P361}`,
'limit': 1,
'fmt': 'json'
}
})
.then(resP175EqArtist => {
axios
.get(`${musicbrainzBaseUrl}/recording/${resP175EqArtist.data.recordings[0].id}`, {
params: {
'inc': 'work-rels+artists+releases',
'fmt': 'json'
}
})
.then(resRecordingQuery => {
let x = resRecordingQuery.data.relations.map(x => x['target-type'] === 'work' ? x.work.id : "").toString();
console.log(x,'resRecordingQuery.data true', resRecordingQuery.data)
}, err => { console.error(err) })
}
, err => console.error(err))
}
else if (P175.toLowerCase() === title.trim().toLowerCase()) { // video title was title - artist
// axios
// .get(`${musicbrainzBaseUrl}/release-group/`)
// .then(res => { }, err => { })
axios
.get(`https://musicbrainz.org/ws/2/recording`, {
params: {
'query': `"${artist.trim()}" AND artistname:"${title.trim()}"`, //rgid:${res.data.}
'limit': 1,
'fmt': 'json'
}
})
.then(resP175EqTitle => {
axios
.get(`${musicbrainzBaseUrl}/recording/${resP175EqTitle.data.recordings[0].id}`, {
params: {
'inc': 'work-rels+artists+releases',
'fmt': 'json'
}
})
.then(resRecordingQuery => {
let x = resRecordingQuery.data.relations.map(x => x['target-type'] === 'work' ? x.work.id : "").toString();
console.log(x, 'resRecordingQuery.data false', resRecordingQuery.data)
}, err => { console.error(err) })
}
, err => console.error(err))
}
}
@@ -62,20 +122,24 @@ class Wikipedia extends Component {
.map(x => wikidatatmp.claims[x][0].mainsnak.datavalue.value.id)
.filter(x => typeof x === 'string'), 'en'))
])
.then(axios.spread((resKey, resQ) => {
this.setState({
wikidatakeys: Object.entries(resKey.data.entities),
wikidataQ: resQ.data.entities,
isWDataLoaded: true
})
}))
.then(
axios.spread((resKey, resQ) => {
this.setState({
wikidatakeys: Object.entries(resKey.data.entities),
wikidataQ: resQ.data.entities,
isWDataLoaded: true
})
this.getMusicBrainz(resQ.data.entities[wikidatatmp.claims.P175[0].mainsnak.datavalue.value.id].labels.en.value,
resQ.data.entities[wikidatatmp.claims.P361[0].mainsnak.datavalue.value.id].labels.en.value);
}))
if (wikidatatmp.sitelinks.enwiki)
wikijs()
.page(wikidatatmp.sitelinks.enwiki.title)
.then(page => Promise.all([page.fullInfo(), page.summary()]))
.then(page => Promise.all([page.fullInfo()]))
.then(wikipediaRes => this.setState({
wikipedia: {
"desc": wikipediaRes[1],
// "desc": wikipediaRes[1],
"info": wikipediaRes[0].general
},
wikipediakeys: Object.keys(wikipediaRes[0].general),
@@ -115,46 +179,47 @@ class Wikipedia extends Component {
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 })
);
});
}
/* 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`, {
else { //oof -- no P1651 :(
// eslint-disable-next-line no-lone-blocks
{/* axios.get(`${musicbrainzBaseUrl}/work`, {
params: {
'query': 'undefined' === typeof this.props.title ? ('undefined' === typeof this.props.title2 ? this.props.artist2 : this.props.title2) : this.props.title,
'limit': 1,
@@ -189,16 +254,20 @@ class Wikipedia extends Component {
}
})
)
})
}) */}
}
return new Promise ((resolve,reject) => {
timerId1 = window.setInterval(()=>{
if((this.state.wikipedia === 'undefined') || (this.state.wikidataQ === 'undefined') || (this.state.wikidata === 'undefined'))
return new Promise((resolve, reject) => {
timerId1 = window.setInterval(() => {
if (this.state.wikidataQ == undefined || this.state.wikidata == undefined) { }
else {
resolve({
artist: this.state.wikipedia.info.artist.toString(),
genre: this.state.wikidataQ[this.state.wikidata.claims["P136"][0].mainsnak.datavalue.value.id].labels.en.value.toString()
artist: this.state.wikidataQ[this.state.wikidata.claims["P175"][0].mainsnak.datavalue.value.id].labels.en.value.toString(),
genre: this.state.wikidataQ[this.state.wikidata.claims["P136"][0].mainsnak.datavalue.value.id].labels.en.value.toString()
})
}, 700)
}
}, 1000)
})
}).then((res) => {
let tmp = JSON.parse(localStorage['currentVideoInfo']);
@@ -208,9 +277,9 @@ class Wikipedia extends Component {
artist: res.artist,
genre: res.genre
}
localStorage.setItem('currentVideoInfo',JSON.stringify(tmp));
localStorage.setItem('currentVideoInfo', JSON.stringify(tmp));
})
.finally(()=>this.setState({ isLoaded: true }));
.finally(() => this.setState({ isLoaded: true }));
}
@@ -233,8 +302,8 @@ class Wikipedia extends Component {
return <React.Fragment>Error: {error.message}</React.Fragment>;
} else if (!this.state.isLoaded) {
return <Col className="text-center">
<div className="lds-ellipsis"><div></div><div></div><div></div><div></div></div>
</Col>;
<div className="lds-ellipsis"><div></div><div></div><div></div><div></div></div>
</Col>;
} else {
return (
<Table size="sm">
@@ -255,7 +324,7 @@ class Wikipedia extends Component {
</td>
<td>{
typeof wikidata.claims[key[0]][0].mainsnak.datavalue.value === "string" ? // wikidata external
this.getExternalLink(key[0],wikidata.claims[key[0]][0].mainsnak.datavalue.value) :
this.getExternalLink(key[0], wikidata.claims[key[0]][0].mainsnak.datavalue.value) :
typeof wikidata.claims[key[0]][0].mainsnak.datavalue.value.id === "string" ? // wikidata Q
<><a target="_blank" rel="noopener noreferrer" href={`https://www.wikidata.org/wiki/${wikidata.claims[key[0]][0].mainsnak.datavalue.value.id}`}>
{`${this.state.wikidataQ[wikidata.claims[key[0]][0].mainsnak.datavalue.value.id].labels.en ?
+27
View File
@@ -111,6 +111,33 @@ class fvitali extends React.Component {
</React.Fragment>
);
// }else{
// return(
// <ul className="list-unstyled">{
// items.map(item => (
// <Link
// key={item.videoID}
// as="li" className="media mx-5 my-1 suggItem"
// to={{
// pathname: `/video/${item.videoID}`,
// search: '?ref=fvitali'
// }}>
// <img
// width={240}
// height={120}
// className="align-self-center mr-3 img-fluid"
// src={item.thumbnail}
// alt={'Thumbnail of ' + item.name}
// />
// <Media.Body>
// <h5>{item.name}</h5>
// <p>{item.lastSelected}</p>
// <p>Reccomender: fvitali</p>
// </Media.Body>
// </Link>
// ))
// }</ul>
// );}
}
}
}