Skip to content

Commit

Permalink
Merge pull request #604 from dularion/main/AE/implement-bulk-api-caching
Browse files Browse the repository at this point in the history
implement bulk api caching
  • Loading branch information
dularion committed Apr 5, 2018
2 parents 9542621 + 5f89bb3 commit 026c403
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 45 deletions.
Expand Up @@ -99,6 +99,7 @@ function modalCreateFromFileCtrl($scope, $uibModalInstance, apiService, uploadSe

function runMatcher() {
vm.isMatcherLoading = true;
vm.matchResult = null;
var fileSelection = _.filter(vm.selection, {directory: false});
apiService.file.matchMetaDataFromFiles(fileSelection).success(function (data) {
vm.isMatcherLoading = false;
Expand Down Expand Up @@ -138,7 +139,11 @@ function modalCreateFromFileCtrl($scope, $uibModalInstance, apiService, uploadSe
}

apiService.file.bulkAddMediaFromFile(allFoundMatches).success(function (result) {
alertify.success("All matches have been added to the database and files connected");
if(_.some(result, {status: 4})){
alertify.log("not all files were added unfortunately. This is due to TheMovieDB API LIMIT constraints. We are sorry and we hope to have a fix soon. ")
}else{
alertify.success("All matches have been added to the database and files connected");
}
mergeMatchResults(result);
});

Expand All @@ -147,7 +152,11 @@ function modalCreateFromFileCtrl($scope, $uibModalInstance, apiService, uploadSe
function addSelectedFile(file) {
var fileMatch = _.find(vm.matchResult, {"file": file.path});
apiService.file.bulkAddMediaFromFile([fileMatch]).success(function (result) {
alertify.success(fileMatch.title || fileMatch.episodeName + " has been added");
if(_.some(result, {status: 4})){
alertify.log("not all files were added unfortunately. This is due to TheMovieDB API LIMIT constraints. We are sorry and we hope to have a fix soon. ")
}else{
alertify.success(fileMatch.title || fileMatch.episodeName + " has been added");
}
mergeMatchResults(result);
});
}
Expand Down
Expand Up @@ -31,6 +31,7 @@
<i class="ion-document"></i> {{ file.name }}
</div>
<div class="col-md-3">
<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> Preview Match</a>
</span>
Expand Down
6 changes: 6 additions & 0 deletions grails-app/assets/stylesheets/_admin.scss
Expand Up @@ -113,6 +113,12 @@
padding: 7px 0;
border-top: 1px solid #555a5c;
}

.column-loading{
width: 1em;
height: 1em;
display: inline-block;
}
}

.upload-poster{
Expand Down
108 changes: 67 additions & 41 deletions grails-app/services/streama/BulkCreateService.groovy
Expand Up @@ -105,49 +105,33 @@ class BulkCreateService {
def name = tvShowMatcher.group('Name').replaceAll(/[._]/, " ")
def seasonNumber = tvShowMatcher.group('Season').toInteger()
def episodeNumber = tvShowMatcher.group('Episode').toInteger()
def type = "tv"
fileResult.type = "tv"

try {
def json = theMovieDbService.searchForEntry(type, name)
def movieDbResults = json?.results

if (movieDbResults) {
// Why do i need to access index 0? Worked just fine without before extracting to service
def tvShowId = movieDbResults.id[0]

if(!seasonNumber && !episodeNumber){
TvShow existingTvShow = TvShow.findByApiIdAndDeletedNotEqual(tvShowId, true)
if(existingTvShow){
fileResult.status = MATCHER_STATUS.EXISTING
fileResult.importedId =existingTvShow.id
fileResult.importedType = STREAMA_ROUTES[type]
}
fileResult.apiId = tvShowId
}


fileResult.tvShowOverview = movieDbResults.overview[0]
fileResult.tvShowId = tvShowId
fileResult.showName = movieDbResults.name[0]
fileResult.poster_path = movieDbResults.poster_path[0]
fileResult.backdrop_path = movieDbResults.backdrop_path[0]

if(seasonNumber && episodeNumber){
type = 'episode'
def episodeResult = theMovieDbService.getEpisodeMeta(tvShowId, seasonNumber, episodeNumber)
Episode existingEpisode = Episode.findByApiIdAndDeletedNotEqual(episodeResult.id, true)
if(existingEpisode){
fileResult.status = MATCHER_STATUS.EXISTING
fileResult.importedId =existingEpisode.showId
fileResult.importedType = STREAMA_ROUTES[type]
}

fileResult.apiId = episodeResult.id
fileResult.episodeName = episodeResult.name
fileResult.first_air_date = episodeResult.air_date
fileResult.episodeOverview = episodeResult.overview
fileResult.still_path = episodeResult.still_path
TvShow existingTvShow
def tvShowData
def tvShowId

def json = theMovieDbService.searchForEntry(fileResult.type, name)
tvShowData = json?.results[0]
tvShowId = tvShowData.id
existingTvShow = TvShow.findByApiIdAndDeletedNotEqual(tvShowId, true)

fileResult.tvShowOverview = tvShowData.overview
fileResult.tvShowId = tvShowId
fileResult.showName = tvShowData.name
fileResult.poster_path = tvShowData.poster_path
fileResult.backdrop_path = tvShowData.backdrop_path

if(!seasonNumber && !episodeNumber){
if(existingTvShow){
fileResult.status = MATCHER_STATUS.EXISTING
fileResult.importedId =existingTvShow.id
fileResult.importedType = STREAMA_ROUTES[fileResult.type]
}
fileResult.apiId = tvShowId
} else {
fileResult = extractDataForEpisode(existingTvShow, seasonNumber, episodeNumber, fileResult, tvShowId)
}
} catch (Exception ex) {
log.error("Error occured while trying to retrieve data from TheMovieDB. Please check your API-Key.")
Expand All @@ -156,11 +140,48 @@ class BulkCreateService {
}
fileResult.status = fileResult.status ?: MATCHER_STATUS.MATCH_FOUND
fileResult.message = 'match found'
fileResult.type = type
fileResult.type = fileResult.type
fileResult.season = seasonNumber
fileResult.episodeNumber = episodeNumber
}

private extractDataForEpisode(TvShow existingTvShow, seasonNumber, episodeNumber, fileResult, tvShowId) {
fileResult.type = 'episode'
Episode existingEpisode

if (existingTvShow) {
existingEpisode = Episode.where {
show == existingTvShow
season_number == seasonNumber
episode_number == episodeNumber
deleted != true
}.get()
}

if (existingEpisode) {
fileResult.status = MATCHER_STATUS.EXISTING
fileResult.importedId = existingEpisode.showId
fileResult.importedType = STREAMA_ROUTES[fileResult.type]
fileResult.apiId = existingEpisode.apiId
}
else {
def episodeResult = theMovieDbService.getEpisodeMeta(tvShowId, seasonNumber, episodeNumber)
existingEpisode = Episode.findByApiIdAndDeletedNotEqual(episodeResult.id, true)
if (existingEpisode) {
fileResult.status = MATCHER_STATUS.EXISTING
fileResult.importedId = existingEpisode.showId
fileResult.importedType = STREAMA_ROUTES[fileResult.type]
}

fileResult.apiId = episodeResult.id
fileResult.episodeName = episodeResult.name
fileResult.first_air_date = episodeResult.air_date
fileResult.episodeOverview = episodeResult.overview
fileResult.still_path = episodeResult.still_path
}
return fileResult
}


def bulkAddMediaFromFile(List<Map> fileMatchers){
def result = []
Expand All @@ -171,6 +192,11 @@ class BulkCreateService {
}

def entity = theMovieDbService.createEntityFromApiId(type, fileMatcher.apiId, fileMatcher)
if(!entity){
fileMatcher.status = MATCHER_STATUS.LIMIT_REACHED
result.add(fileMatcher)
return
}
if(entity instanceof Video){
entity.addLocalFile(fileMatcher.file)
}
Expand Down
19 changes: 17 additions & 2 deletions grails-app/services/streama/TheMovieDbService.groovy
Expand Up @@ -8,6 +8,8 @@ class TheMovieDbService {

def BASE_URL = "https://api.themoviedb.org/3"

def apiCacheData = [:]

def getAPI_PARAMS(){
return "api_key=$API_KEY&language=$API_LANGUAGE"
}
Expand Down Expand Up @@ -124,10 +126,18 @@ class TheMovieDbService {
}

def searchForEntry(type, name) {

def cachedApiData = apiCacheData."$type:$name"
if(cachedApiData){
return cachedApiData
}
def query = URLEncoder.encode(name, "UTF-8")

def JsonContent = new URL(BASE_URL + '/search/' + type + '?query=' + query + '&api_key=' + API_KEY).getText("UTF-8")
return new JsonSlurper().parseText(JsonContent)
def data = new JsonSlurper().parseText(JsonContent)
apiCacheData["$type:$name"] = data

return data
}

def getEntryById(String type, id, data = [:]){
Expand All @@ -148,7 +158,12 @@ class TheMovieDbService {

def createEntityFromApiId(type, id, data = [:]){
def apiData = getEntryById(type, id, data)
def entity = createEntityFromApiData(type, apiData)
def entity
try{
entity = createEntityFromApiData(type, apiData)
}catch (e){
log.error("Error occured while trying to retrieve data from TheMovieDB. Please check your API-Key.")
}
return entity
}

Expand Down

0 comments on commit 026c403

Please sign in to comment.