From 3402c7fa3bf777ac93b69593245636467d8264ec Mon Sep 17 00:00:00 2001 From: lukas Date: Mon, 8 Jun 2020 17:52:58 +0200 Subject: [PATCH] parse genres from tmdb popup to add a new tag manually added some key props to prevent warnings --- api/Database.php | 20 ++++++++++ api/TMDBMovie.php | 3 -- api/Tags.php | 12 +++++- api/extractvideopreviews.php | 60 ++++++++++++++++++++++++++-- src/elements/AddTagPopup.js | 2 +- src/elements/NewTagPopup.js | 76 ++++++++++++++++++++++++++++++++++++ src/elements/Preview.js | 2 +- src/pages/CategoryPage.js | 34 ++++++++++++---- src/pages/Player.js | 2 +- src/pages/SettingsPage.js | 2 +- 10 files changed, 194 insertions(+), 19 deletions(-) create mode 100644 src/elements/NewTagPopup.js diff --git a/api/Database.php b/api/Database.php index 583289f..f12d6ac 100644 --- a/api/Database.php +++ b/api/Database.php @@ -1,5 +1,10 @@ conn; } + /** + * get name of current active database + * @return string name + */ public function getDatabaseName(){ return $this->dbname; } diff --git a/api/TMDBMovie.php b/api/TMDBMovie.php index be73f93..e4fe023 100644 --- a/api/TMDBMovie.php +++ b/api/TMDBMovie.php @@ -35,6 +35,3 @@ class TMDBMovie return $reply->genres; } } - -$temp = new TMDBMovie(); -$temp->searchMovie("007 - Ein quantum Trost"); diff --git a/api/Tags.php b/api/Tags.php index 1c96582..fcc63b7 100644 --- a/api/Tags.php +++ b/api/Tags.php @@ -17,5 +17,15 @@ if (isset($_POST['action'])) { echo json_encode($rows); break; + + case "createTag": + $query = "INSERT INTO tags (tag_name) VALUES ('" . $_POST['tagname'] . "')"; + + if ($conn->query($query) === TRUE) { + echo('{"result":"success"}'); + } else { + echo('{"result":"' . $conn->error . '"}'); + } + break; } -} \ No newline at end of file +} diff --git a/api/extractvideopreviews.php b/api/extractvideopreviews.php index 3061284..033491e 100755 --- a/api/extractvideopreviews.php +++ b/api/extractvideopreviews.php @@ -6,6 +6,8 @@ writeLog("starting extraction!\n"); $ffmpeg = 'ffmpeg'; //or: /usr/bin/ffmpeg , or /usr/local/bin/ffmpeg - depends on your installation (type which ffmpeg into a console to find the install path) $tmdb = new TMDBMovie(); +// initial load off all available movie genres +$tmdbgenres = $tmdb->getAllGenres(); $conn = Database::getInstance()->getConnection(); @@ -82,6 +84,8 @@ foreach ($arr as $elem) { if ($conn->query($query) === TRUE) { echo('successfully added ' . $elem . " to video gravity\n"); writeLog('successfully added ' . $elem . " to video gravity\n"); + + // add this entry to the default tags $last_id = $conn->insert_id; // full hd @@ -93,6 +97,7 @@ foreach ($arr as $elem) { } } + // HD if ($width >= 1250 && $width < 1900) { $query = "INSERT INTO video_tags(video_id,tag_id) VALUES ($last_id,4)"; if ($conn->query($query) !== TRUE) { @@ -101,6 +106,7 @@ foreach ($arr as $elem) { } } + // SD if ($width < 1250 && $width > 0) { $query = "INSERT INTO video_tags(video_id,tag_id) VALUES ($last_id,3)"; if ($conn->query($query) !== TRUE) { @@ -111,9 +117,17 @@ foreach ($arr as $elem) { // handle tmdb genres here! if ($genres != -1) { - foreach ($genres as $genre) { - // check if genre is already a tag in db -// echo $genre."\n\n"; + // transform genre ids in valid names + foreach ($genres as $genreid) { + // check if genre is already a tag in db if not insert it + $tagname = array_column($tmdbgenres, 'name', 'id')[$genreid]; + $tagid = tagExists($tagname); + + $query = "INSERT INTO video_tags(video_id,tag_id) VALUES ($last_id,$tagid)"; + if ($conn->query($query) !== TRUE) { + echo "failed to add $genreid tag here.\n"; + writeLog("failed to add $genreid tag here.\n"); + } } } @@ -137,6 +151,7 @@ foreach ($arr as $elem) { } } +// auto cleanup db entries $query = "SELECT COUNT(*) as count FROM videos"; $result = $conn->query($query); $r = mysqli_fetch_assoc($result); @@ -190,8 +205,15 @@ echo "deleted in this run: " . $deleted . "\n"; writeLog("deleted in this run: " . $deleted . "\n"); echo "errored in this run: " . $failed . "\n"; writeLog("errored in this run: " . $failed . "\n"); -writeLog("-42"); +writeLog("-42"); // terminating characters to stop webui requesting infos + +/** + * get all videoinfos of a video file + * + * @param $video string name including extension + * @return object all infos as object + */ function _get_video_attributes($video) { $command = "mediainfo \"../videos/prn/$video\" --Output=JSON"; @@ -199,8 +221,38 @@ function _get_video_attributes($video) return json_decode($output); } +/** + * write a line to the output log file + * + * @param string $message message to write + */ function writeLog(string $message) { file_put_contents("/tmp/output.log", $message, FILE_APPEND); flush(); } + +/** + * ckecks if tag exists -- if not creates it + * @param string $tagname the name of the tag + * @return integer the id of the inserted tag + */ +function tagExists(string $tagname) +{ + global $conn; + + $query = "SELECT * FROM tags WHERE tag_name='$tagname'"; + + $result = $conn->query($query); + if ($result->num_rows == 0) { + // tag does not exist --> create it + $query = "INSERT INTO tags (tag_name) VALUES ('$tagname')"; + if ($conn->query($query) !== TRUE) { + echo "failed to create $tagname tag in database\n"; + writeLog("failed to create $tagname tag in database\n"); + } + return $conn->insert_id; + } else { + return $result->fetch_assoc()['tag_id']; + } +} diff --git a/src/elements/AddTagPopup.js b/src/elements/AddTagPopup.js index 17abd06..7f0d2fa 100644 --- a/src/elements/AddTagPopup.js +++ b/src/elements/AddTagPopup.js @@ -50,7 +50,7 @@ class AddTagPopup extends React.Component { {this.state.items ? this.state.items.map((i) => ( - { + { this.setState({selection: {name: i.tag_name, id: i.tag_id}}) }}>{i.tag_name} )) : diff --git a/src/elements/NewTagPopup.js b/src/elements/NewTagPopup.js new file mode 100644 index 0000000..e1f01c0 --- /dev/null +++ b/src/elements/NewTagPopup.js @@ -0,0 +1,76 @@ +import React from "react"; +import Modal from 'react-bootstrap/Modal' +import {Form} from "react-bootstrap"; + +class NewTagPopup extends React.Component { + constructor(props, context) { + super(props, context); + + this.props = props; + } + + componentDidMount() { + const updateRequest = new FormData(); + updateRequest.append('action', 'getAllTags'); + + fetch('/api/videoload.php', {method: 'POST', body: updateRequest}) + .then((response) => response.json()) + .then((result) => { + this.setState({ + items: result + }); + }); + } + + render() { + return ( + <> + + + + Create a new Tag! + + + + + Tag Name: + + + This Tag will automatically show up on category page. + + + + + + + + + ); + } + + storeselection() { + const updateRequest = new FormData(); + updateRequest.append('action', 'createTag'); + updateRequest.append('tagname', document.getElementById("namefield").value); + + fetch('/api/Tags.php', {method: 'POST', body: updateRequest}) + .then((response) => response.json()) + .then((result) => { + if (result.result !== "success") { + console.log("error occured while writing to db -- todo error handling"); + console.log(result.result); + } + this.props.onHide(); + }); + } +} + +export default NewTagPopup; diff --git a/src/elements/Preview.js b/src/elements/Preview.js index 0b95e1a..ee14db1 100644 --- a/src/elements/Preview.js +++ b/src/elements/Preview.js @@ -85,7 +85,7 @@ export class TagPreview extends React.Component { this.props.categorybinding( + viewbinding={this.props.viewbinding}/>,tag ); })) .catch(() => { diff --git a/src/pages/CategoryPage.js b/src/pages/CategoryPage.js index eb6d742..986636b 100644 --- a/src/pages/CategoryPage.js +++ b/src/pages/CategoryPage.js @@ -3,6 +3,7 @@ import SideBar from "../elements/SideBar"; import Tag from "../elements/Tag"; import {TagPreview} from "../elements/Preview"; +import NewTagPopup from "../elements/NewTagPopup"; class CategoryPage extends React.Component { constructor(props, context) { @@ -12,7 +13,7 @@ class CategoryPage extends React.Component { this.state = { loadedtags: [], - selected: false + selected: null }; } @@ -24,9 +25,9 @@ class CategoryPage extends React.Component { return ( <>
- Category Page + Categories {this.state.loadedtags ? this.state.loadedtags.length + " different Tags" : ""} + className='pageheadersubtitle'>{!this.state.selected ? this.state.loadedtags.length + " different Tags" : this.state.selected}
@@ -36,7 +37,10 @@ class CategoryPage extends React.Component { LowQuality HD
- +
{!this.state.selected ? @@ -44,6 +48,7 @@ class CategoryPage extends React.Component { {this.state.loadedtags ? this.state.loadedtags.map((m) => ( ) : - this.selectionelements + <> + {this.selectionelements} + + } + {this.state.popupvisible ? + { + this.setState({popupvisible: false}); + this.loadTags(); + }}/> : + null + } ); } - setPage = (element) => { + setPage = (element, tagname) => { this.selectionelements = element; - this.setState({selected: true}); + this.setState({selected: tagname}); + }; + + loadCategoryPageDefault = () => { + this.setState({selected: null}); }; /** diff --git a/src/pages/Player.js b/src/pages/Player.js index 294e674..0e9a783 100644 --- a/src/pages/Player.js +++ b/src/pages/Player.js @@ -66,7 +66,7 @@ class Player extends React.Component {
Tags:
{this.state.tags.map((m) => ( - {m.tag_name} + {m.tag_name} ))} diff --git a/src/pages/SettingsPage.js b/src/pages/SettingsPage.js index 895bd51..3629063 100644 --- a/src/pages/SettingsPage.js +++ b/src/pages/SettingsPage.js @@ -50,7 +50,7 @@ class SettingsPage extends React.Component {
-