Skip to content

Commit

Permalink
add handling for subtitles for MOVIES for bulkCreation
Browse files Browse the repository at this point in the history
  • Loading branch information
dularion committed May 19, 2019
1 parent ee0dee0 commit 9694af1
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ angular.module('streama')

function modalCreateFromFileCtrl($scope, $uibModalInstance, apiService, uploadService, dialogOptions, modalService, $state) {
var vm = this;
var STATUS_NO_MATCH = 0;
var STATUS_MATCH_FOUND = 1;
var STATUS_EXISTING = 2;
var STATUS_CREATED = 3;
var STATUS_LIMIT_REACHED = 4;
var STATUS_EXISTING_FOR_SUBTITLE = 5;
var STATUS_SUBTITLE_MATCH = 6;
var STATUS_SUBTITLE_ADDED = 7;

vm.loading = false;
vm.localFilesEnabled = false;
vm.localFiles = [];
Expand All @@ -30,6 +39,7 @@ function modalCreateFromFileCtrl($scope, $uibModalInstance, apiService, uploadSe
vm.openMediaDetail = openMediaDetail;
vm.openAdminForm = openAdminForm;
vm.isSelected = isSelected;
vm.hasStatus = hasStatus;


init();
Expand Down Expand Up @@ -109,7 +119,7 @@ function modalCreateFromFileCtrl($scope, $uibModalInstance, apiService, uploadSe
vm.isMatcherLoading = false;
vm.matchResult = data;
//console.log(data);
deselectByStatus(2);
deselectByStatus(STATUS_EXISTING);
});
}

Expand Down Expand Up @@ -162,14 +172,16 @@ function modalCreateFromFileCtrl($scope, $uibModalInstance, apiService, uploadSe


function addAllMatches() {
var allFoundMatches = _.filter(vm.matchResult, {status: 1});
var allFoundMatches = _.filter(vm.matchResult, function (match) {
return (match.status === STATUS_MATCH_FOUND || match.status === STATUS_EXISTING_FOR_SUBTITLE || match.status === STATUS_SUBTITLE_MATCH)
});
if(allFoundMatches.length == 0){
alertify.success('Nothing to add.');
}

apiService.file.bulkAddMediaFromFile(allFoundMatches).then(function (response) {
var data = response.data;
if(_.some(data, {status: 4})){
if(_.some(data, {status: STATUS_LIMIT_REACHED})){
alertify.log("not all files were added unfortunately. This is due to TheMovieDB API LIMIT constraints. Just try again in a couple of seconds :). ")
}else{
alertify.success("All matches have been added to the database and files connected");
Expand All @@ -183,7 +195,7 @@ function modalCreateFromFileCtrl($scope, $uibModalInstance, apiService, uploadSe
var fileMatch = _.find(vm.matchResult, {"file": file.path});
apiService.file.bulkAddMediaFromFile([fileMatch]).then(function (response) {
var result = response.data;
if(_.some(result, {status: 4})){
if(_.some(result, {status: STATUS_LIMIT_REACHED})){
alertify.log("not all files were added unfortunately. This is due to TheMovieDB API LIMIT constraints. Just try again in a couple of seconds :) ")
}else{
alertify.success(fileMatch.title || fileMatch.episodeName + " has been added");
Expand All @@ -197,7 +209,7 @@ function modalCreateFromFileCtrl($scope, $uibModalInstance, apiService, uploadSe
return _.find(result, {file: match.file}) || match;
});

deselectByStatus(3);
deselectByStatus(STATUS_CREATED);
}


Expand All @@ -208,4 +220,12 @@ function modalCreateFromFileCtrl($scope, $uibModalInstance, apiService, uploadSe
});
}

function hasStatus(file, status) {
var matchForPath = getMatchForPath(file.path);
if(!matchForPath){
return false;
}
return (matchForPath.status === status);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<span ng-if="vm.getMatchForPath(file.path)">
{{vm.getMatchForPath(file.path).message}}
</span>
<button class="btn btn-xs btn-success button-icon" ng-if="vm.getMatchForPath(file.path).status == 1"
<button class="btn btn-xs btn-success button-icon" ng-if="vm.hasStatus(file, 1)"
ng-click="vm.addSelectedFile()">Add this file
</button>
</div>
Expand All @@ -31,30 +31,46 @@
<i class="ion-document"></i> {{ file.name }}
</div>
<div class="col-md-3" style="padding-right: 0;">
<i class="ion-load-c spin column-loading" ng-show="vm.isMatcherLoading && vm.isSelected(file) && (!vm.getMatchForPath(file.path) || vm.getMatchForPath(file.path).status == 1)"></i>
<span ng-if="vm.getMatchForPath(file.path) && vm.getMatchForPath(file.path).status == 1">
<a ng-click="vm.openMediaDetail(vm.getMatchForPath(file.path))"><i class="ion-ios-eye"></i> Match: {{vm.getMatchDisplay(file)}}</a>
<i class="ion-load-c spin column-loading"
ng-show="vm.isMatcherLoading && vm.isSelected(file) && (!vm.getMatchForPath(file.path) || vm.hasStatus(file, 1))"></i>
<span ng-if="vm.getMatchForPath(file.path) && (vm.hasStatus(file, 1) || vm.hasStatus(file, 6))">
<a ng-click="vm.openMediaDetail(vm.getMatchForPath(file.path))">
<i class="ion-ios-eye"></i>
{{vm.hasStatus(file, 6) ? 'Subtitle': ''}} Match: {{vm.getMatchDisplay(file)}}
</a>
</span>
<span ng-if="vm.getMatchForPath(file.path) && vm.getMatchForPath(file.path).status == 2">
<span ng-if="vm.getMatchForPath(file.path) && vm.hasStatus(file, 2)">
already added
</span>
<span ng-if="vm.getMatchForPath(file.path) && vm.getMatchForPath(file.path).status == 3">
<span ng-if="vm.getMatchForPath(file.path) && vm.hasStatus(file, 3)">
created
</span>
<span ng-if="vm.getMatchForPath(file.path) && vm.getMatchForPath(file.path).status == 0">
<span ng-if="vm.getMatchForPath(file.path) && vm.hasStatus(file, 7)">
subtitle added
</span>
<span ng-if="vm.getMatchForPath(file.path) && vm.hasStatus(file, 5)">
found Media for Subtitle:
<a ng-click="vm.openAdminForm(vm.getMatchForPath(file.path))"
class="text-success">{{vm.getMatchDisplay(file)}} <i class="ion-android-open"></i></a>

</span>
<span ng-if="vm.getMatchForPath(file.path) && vm.hasStatus(file, 0)">
{{vm.getMatchForPath(file.path).message}}
</span>
<span class="text-danger" ng-if="vm.getMatchForPath(file.path) && vm.getMatchForPath(file.path).status == 4">
<span class="text-danger" ng-if="vm.getMatchForPath(file.path) && vm.hasStatus(file, 4)">
TheMovieDB Error: &nbsp;<i class="ion-help-circled" title="{{vm.getMatchForPath(file.path).errorMessage}}"></i>
</span>

<br>

<button class="btn btn-xs btn-success button-icon" ng-if="vm.getMatchForPath(file.path).status == 1"
<button class="btn btn-xs btn-success button-icon" ng-if="vm.hasStatus(file, 1)"
ng-click="vm.addSelectedFile(file)">Add this file
</button>
<a ng-if="vm.getMatchForPath(file.path).status == 2 || vm.getMatchForPath(file.path).status == 3"
<button class="btn btn-xs btn-success button-icon" ng-if="vm.hasStatus(file, 5)"
ng-click="vm.addSelectedFile(file)">Add subtitle file
</button>
<a ng-if="vm.hasStatus(file, 2) || vm.hasStatus(file, 3) || vm.hasStatus(file, 7)"
ng-click="vm.openAdminForm(vm.getMatchForPath(file.path))"
class="text-success">Match: {{vm.getMatchDisplay(file)}} <i class="ion-android-open"></i></a>
class="text-success">Go to: {{vm.getMatchDisplay(file)}} <i class="ion-android-open"></i></a>
</div>
</div>
43 changes: 40 additions & 3 deletions grails-app/services/streama/BulkCreateService.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ class BulkCreateService {
MATCH_FOUND: 1,
EXISTING: 2,
CREATED: 3,
LIMIT_REACHED: 4
LIMIT_REACHED: 4,
EXISTING_FOR_SUBTITLE: 5,
SUBTITLE_MATCH: 6,
SUBTITLE_ADDED: 7
]
final static STREAMA_ROUTES = [
movie: 'movie',
Expand Down Expand Up @@ -82,6 +85,7 @@ class BulkCreateService {
def name = movieMatcher.group('Name').replaceAll(/[._]/, " ")
def year = movieRegex.contains('<Year>') ? movieMatcher.group('Year') : null
def type = "movie"
Boolean isSubtitle = VideoHelper.isSubtitleFile(fileResult.file)

try {
def json = theMovieDbService.searchForEntry(type, name, year)
Expand All @@ -94,7 +98,11 @@ class BulkCreateService {

Movie existingMovie = Movie.findByApiIdAndDeletedNotEqual(movieResult.id, true)
if(existingMovie){
fileResult.status = MATCHER_STATUS.EXISTING
if(isSubtitle){
fileResult.status = MATCHER_STATUS.EXISTING_FOR_SUBTITLE
}else{
fileResult.status = MATCHER_STATUS.EXISTING
}
fileResult.importedId = existingMovie.id
fileResult.importedType = STREAMA_ROUTES[type]
}
Expand All @@ -113,6 +121,9 @@ class BulkCreateService {
fileResult.title = name
}
fileResult.status = fileResult.status ?: MATCHER_STATUS.MATCH_FOUND
if(fileResult.status == MATCHER_STATUS.MATCH_FOUND && isSubtitle){
fileResult.status = MATCHER_STATUS.SUBTITLE_MATCH
}
fileResult.message = 'match found'
fileResult.type = type
}
Expand Down Expand Up @@ -204,7 +215,10 @@ class BulkCreateService {
@NotTransactional
def bulkAddMediaFromFile(List<Map> fileMatchers){
def result = []
fileMatchers.each{ fileMatcher ->
List<Map> videoFiles = fileMatchers.findAll{!VideoHelper.isSubtitleFile(it.file)}
List<Map> subtitleFiles = fileMatchers.findAll{VideoHelper.isSubtitleFile(it.file)}

videoFiles.each{ fileMatcher ->
String type = fileMatcher.type
def entity
if(fileMatcher.status == MATCHER_STATUS.EXISTING){
Expand Down Expand Up @@ -233,6 +247,29 @@ class BulkCreateService {
result.add(fileMatcher)
}

subtitleFiles.each{ fileMatcher ->
String type = fileMatcher.type
Video videoInstance

if(type == 'movie'){
videoInstance = Movie.findByApiIdAndDeletedNotEqual(fileMatcher.apiId, true)
}
else if(type == 'episode'){
videoInstance = Episode.findByApiIdAndDeletedNotEqual(fileMatcher.apiId, true)
}

if(!videoInstance){
log.error("no video found for subtitle")
return
}
videoInstance.addLocalFile(fileMatcher.file)

fileMatcher.status = MATCHER_STATUS.SUBTITLE_ADDED
fileMatcher.importedId = videoInstance instanceof Episode ? videoInstance.showId : videoInstance.id
fileMatcher.importedType = STREAMA_ROUTES[type]
result.add(fileMatcher)
}

return result
}
}
16 changes: 16 additions & 0 deletions src/main/groovy/streama/VideoHelper.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package streama

class VideoHelper {

static String getExtensionFromFilename(String filename){
Integer extensionIndex = filename.lastIndexOf('.')
String extension = filename[extensionIndex..-1]

return extension
}

static Boolean isSubtitleFile(String filename){
String extension = getExtensionFromFilename(filename)
return (extension == '.srt' || extension == '.vtt')
}
}

0 comments on commit 9694af1

Please sign in to comment.