Skip to content

Commit

Permalink
Merge pull request #515 from YvonTre/feat(subtitle)-hints-info
Browse files Browse the repository at this point in the history
Feat(subtitle): hints info
  • Loading branch information
ipy authored Mar 7, 2019
2 parents 779bfaa + 941992d commit 2553760
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@
"p-queue": "^3.1.0",
"rimraf": "^2.6.3",
"romanize": "^1.1.1",
"sagi-api": "^0.0.7",
"sagi-api": "^0.0.9",
"source-map-support": "^0.5.9",
"subtitle": "^2.0.1",
"traditional-or-simplified": "^1.0.3",
Expand Down
19 changes: 18 additions & 1 deletion src/renderer/components/Subtitle/SubtitleManager.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<script>
import { mapGetters, mapActions, mapState } from 'vuex';
import romanize from 'romanize';
import { sep } from 'path';
import { flatten, isEqual, sortBy, differenceWith, isFunction, partial, pick, values, keyBy, mergeWith, castArray } from 'lodash';
import { codeToLanguageName } from '@/helpers/language';
import {
Expand Down Expand Up @@ -206,7 +207,8 @@ export default {
return storedSubs;
}
}
const fetchSubs = lang => fetchOnlineList(videoSrc, lang).catch(() => []);
const hints = this.generateHints(videoSrc);
const fetchSubs = lang => fetchOnlineList(videoSrc, lang, hints).catch(() => []);
const newSubs = await Promise.all(languages.map(fetchSubs)).then(flatten);
deleteSubtitles(storedSubIds);
return newSubs;
Expand Down Expand Up @@ -520,6 +522,21 @@ export default {
}
return result;
},
generateHints(videoSrc) {
let result;
videoSrc.split(sep).reverse().some((dirOrFileName, index) => {
if (index === 0) {
result = dirOrFileName;
return false;
} else if (index <= 2) {
result = `${dirOrFileName}${sep}${result}`;
return false;
}
result = `${sep}${result}`;
return true;
});
return result;
},
},
created() {
this.$bus.$on('add-subtitles', (subs) => {
Expand Down
7 changes: 4 additions & 3 deletions src/renderer/helpers/sagi.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,13 @@ class Sagi {
this.transcripts = [];
}

mediaTranslateRaw(mediaIdentity, languageCode) {
mediaTranslateRaw(mediaIdentity, languageCode, hints) {
return new Promise((resolve, reject) => {
const client = new translationRpc.TranslationClient(this.endpoint, this.creds);
const req = new translationMsg.MediaTranslationRequest();
req.setMediaIdentity(mediaIdentity);
req.setLanguageCode(languageCode);
req.setHints(hints);
client.translateMedia(req, (err, res) => {
if (err) {
reject(err);
Expand All @@ -62,13 +63,13 @@ class Sagi {
});
}

mediaTranslate({ mediaIdentity, languageCode }) {
mediaTranslate({ mediaIdentity, languageCode, hints }) {
if (!languageCode) {
languageCode = 'zh';
console.warn('No languageCode provided, falling back to zh.');
}
return new Promise((resolve, reject) => {
this.mediaTranslateRaw(mediaIdentity, languageCode).then((response) => {
this.mediaTranslateRaw(mediaIdentity, languageCode, hints).then((response) => {
const { error, resultsList } = response.toObject();
if (error && error.code) reject(error);
resolve(resultsList);
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/helpers/subtitle/searchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export function searchForLocalList(videoSrc, supportedExtensions) {
});
}

export function fetchOnlineList(videoSrc, languageCode) {
export function fetchOnlineList(videoSrc, languageCode, hints) {
const subtitleInfoNormalizer = (subtitle) => {
const { languageCode: code, transcriptIdentity: src, ranking } = subtitle;
return ({
Expand All @@ -46,7 +46,7 @@ export function fetchOnlineList(videoSrc, languageCode) {
};
return new Promise((resolve, reject) => {
calculateMediaIdentity(videoSrc)
.then(mediaIdentity => Sagi.mediaTranslate({ mediaIdentity, languageCode }))
.then(mediaIdentity => Sagi.mediaTranslate({ mediaIdentity, languageCode, hints }))
.then(results => resolve(results.map(subtitleInfoNormalizer)))
.catch(err => reject(err));
});
Expand Down
4 changes: 2 additions & 2 deletions test/unit/specs/Subtitle/SubtitleManager.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,8 @@ describe('Subtitle Manager Unit Tests', () => {
const randomLanguages = [randStr(), randStr()];
getOnlineSubtitlesList(videoSrc, true, testStoredSubIds, randomLanguages)
.then(() => {
expect(fetchOnlineListStub).to.have.been.calledWithExactly(videoSrc, randomLanguages[0]);
expect(fetchOnlineListStub).to.have.been.calledWithExactly(videoSrc, randomLanguages[1]);
expect(fetchOnlineListStub).to.have.been.calledWith(videoSrc, randomLanguages[0]);
expect(fetchOnlineListStub).to.have.been.calledWith(videoSrc, randomLanguages[1]);
done();
}).catch(done);
});
Expand Down
2 changes: 1 addition & 1 deletion test/unit/specs/helpers/sagi.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('helper.sagi api', () => {

Sagi.mediaTranslate({ mediaIdentity: randomMediaIdentity });

sandbox.assert.calledWithExactly(mediaTranslateRawSpy, randomMediaIdentity, 'zh');
sandbox.assert.calledWith(mediaTranslateRawSpy, randomMediaIdentity, 'zh');
});

// TODO: error handling tests
Expand Down
2 changes: 1 addition & 1 deletion test/unit/specs/helpers/subtitle/push.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('helper - subtitle - push', () => {
testPQueue = new PQueue();
testTQueue = new TranscriptQueue(testPQueue);
});
describe.only('method - add', () => {
describe('method - add', () => {
let testSubtitle;
let testSubtitleId;
let addStub;
Expand Down
1 change: 1 addition & 0 deletions test/unit/specs/helpers/subtitle/searchers.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ describe('Subtitle Searchers Unit Tests', () => {
sandbox.assert.calledWithExactly(mediaTranslateStub, {
mediaIdentity,
languageCode: undefined,
hints: undefined,
});
done();
}).catch(err => done(err));
Expand Down

0 comments on commit 2553760

Please sign in to comment.