Skip to content

Commit

Permalink
add reload
Browse files Browse the repository at this point in the history
  • Loading branch information
nblthree committed May 31, 2019
1 parent 94da4e2 commit c9aab05
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 54 deletions.
89 changes: 66 additions & 23 deletions main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ ipcMain.on('get-aniList', (event, arg) => {
event.returnValue = store.get('aniList') || []
})
ipcMain.on('start-download', (event, { anime, episode }) => {
startDownloading(episode.hash.magnet, event, anime)
startDownloading(episode.magnet, event, anime)
})

ipcMain.on('get-followedAni', (event, arg) => {
Expand All @@ -47,7 +47,7 @@ ipcMain.on('set-followedAni', async (event, arg) => {
// Episodes
const aniList = store.get('aniList') || []

const { newTitle, newHashes } = await refresh(arg)
const { newTitle, newHashes } = await getAnimeEpisodes(arg)
arg.title = newTitle
const hashes = newHashes

Expand All @@ -56,12 +56,27 @@ ipcMain.on('set-followedAni', async (event, arg) => {
mal_id: arg.mal_id,
title: arg.title,
episodesNumber: arg.episodes,
episodesHash: hashes,
episodes: hashes,
watchedEpisodes: 0
})
store.set('aniList', aniList)
}
})

ipcMain.on('reload-episodes', async (event, arg) => {
let aniList = store.get('aniList') || []
aniList = aniList.filter(val => val.mal_id !== arg.mal_id)
const results = await getHorribleSubs(arg.title)
aniList.push({
mal_id: arg.mal_id,
title: arg.title,
episodesNumber: arg.episodes,
episodes: results,
watchedEpisodes: 0
})
store.set('aniList', aniList)
})

ipcMain.on('unset-followedAni', (event, arg) => {
let followedAni = store.get('followedAni') || []
followedAni = followedAni.filter(val => val.mal_id !== arg.mal_id)
Expand All @@ -72,10 +87,10 @@ ipcMain.on('get-downloadedEpi', async (event, arg) => {
const aniList = store.get('aniList') || []
const torrent = {}
for (const val of aniList) {
for (const _val of val.episodesHash) {
for (const _val of val.episodes) {
if (_val.pathname && (await fs.pathExists(_val.pathname))) {
torrent[_val.hash.magnet] = {
key: _val.hash.magnet,
torrent[_val.magnet] = {
key: _val.magnet,
progress: 1
}
}
Expand All @@ -85,10 +100,10 @@ ipcMain.on('get-downloadedEpi', async (event, arg) => {
event.returnValue = torrent
})
function isDuplicate(array, arg) {
return array.some(val => val.hash.magnet === arg.magnet)
return array.some(val => val.magnet === arg.magnet)
}

function refresh(anime) {
function getAnimeEpisodes(anime) {
return new Promise(async resolve => {
const titleOperations = [
{ name: 'normal' },
Expand All @@ -99,7 +114,7 @@ function refresh(anime) {
const newEpisodesNumber = anime.episodes
const newHashes = []
const loopLength = Array.apply(null, {
length: /* anime.episodes || */ 500
length: 500
}).map(Number.call, Number)
let newTitle = anime.title
for (const operation of titleOperations) {
Expand All @@ -124,14 +139,14 @@ function refresh(anime) {
}

newTitle = newTitle.trim()
console.log(newTitle)

for (const i of loopLength) {
const hash = chooseHash(await getHashes(newTitle, i + 1), {
const item = chooseHash(await getHashes(newTitle, i + 1), {
title: newTitle,
episode: i + 1
})
if (hash && !isDuplicate(newHashes, hash)) {
newHashes.push({ hash, episode: i + 1 })
if (item && !isDuplicate(newHashes, item)) {
newHashes.push({ ...item, number: i + 1 })
} else {
break
}
Expand All @@ -144,7 +159,30 @@ function refresh(anime) {
})
}

async function getHashes(title, episode) {
function getHorribleSubs(title) {
return new Promise(async resolve => {
const newTitle =
'[HorribleSubs] ' +
title
.replace(/season|nd|part|rd|th|s?\d+/gi, '')
.replace(/ +/g, ' ')
.trim()
let allMagnets = []
for (let i = 1; i < 50; i++) {
const result = await getHashes(newTitle, -1, i)
if (result && result.length) {
allMagnets.push(...result)
} else {
break
}
}

allMagnets = allMagnets.map((val, index) => ({ ...val, number: index + 1 }))
resolve(allMagnets.sort((a, b) => a.title.localeCompare(b.title)))
})
}

async function getHashes(title, episode, p = 1) {
const hashes = await new Promise(resolve => {
void (async () => {
try {
Expand All @@ -154,7 +192,7 @@ async function getHashes(title, episode) {
`https://nyaa.si/?f=0&c=1_2&q=${processTitle(
title,
episode
)}&s=seeders&o=desc`
)}&p=${p}&s=seeders&o=desc`
)

const result = await page.evaluate(() => {
Expand Down Expand Up @@ -211,7 +249,12 @@ function chooseHash(hashes, { title, episode }) {

function processTitle(title, episode) {
const quality = 720
title += ' ' + episode + ' ' + quality
if (episode !== -1) {
title += ' ' + episode + ' ' + quality
} else {
title += ' ' + quality
}

title = fixedEncodeURI(title)
return title
}
Expand Down Expand Up @@ -241,19 +284,19 @@ function startDownloading(magnet, event, anime) {
torrent.on('done', function() {
console.log('torrent download finished')
let aniList = store.get('aniList') || []
let { episodesHash } = aniList.filter(
val => val.mal_id === anime.mal_id
)[0]
episodesHash = episodesHash.map(val => {
if (val.hash.magnet === magnetURI) {
val.pathname = pathname + '/' + torrent.files[0].path
let { episodes } = aniList.filter(val => val.mal_id === anime.mal_id)[0]
episodes = episodes.map(val => {
if (val.magnet === magnetURI) {
for (let i = 0; i < torrent.files.length; i++) {
val.pathnames[i] = pathname + '/' + torrent.files[i].path
}
}

return val
})
aniList = aniList.map(val => {
if (val.mal_id === anime.mal_id) {
val.episodesHash = episodesHash
val.episodes = episodes
}

return val
Expand Down
30 changes: 22 additions & 8 deletions renderer/components/CadreEpisodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,22 @@ export default class extends Component {

render() {
return (
<div
className="cadre"
onClick={() => this.props.showEpisodes(this.props.anime)}
>
<div className="cadre">
<div className="state">
{this.state.finished_airing
? 'finished Airing'
: `1/${this.props.anime.episodes}`}
</div>
<div className="img" />
<div
className="reload"
onClick={() => this.props.reload(this.props.anime)}
>
Reload
</div>
<div
className="img"
onClick={() => this.props.showEpisodes(this.props.anime)}
/>
<div className="title">{this.props.anime.title}</div>
<style jsx>{`
.cadre {
Expand All @@ -29,18 +35,26 @@ export default class extends Component {
position: relative;
margin: 0 7px 15px 7px;
}
.state {
.state, .reload {
position: absolute;
top: 0;
left: 0;
width: 60px;
height: 20px;
font-size: 12px;
background-color: blue;
text-align: center;
line-height: 20px;
color: #fff;
}
.state {
top: 0;
left: 0;
background-color: blue;
}
.reload {
right: 0;
background-color: red;
cursor: pointer;
}
.img {
width: 100%;
height: 100%;
Expand Down
48 changes: 25 additions & 23 deletions renderer/pages/followed.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ export default class extends Component {
this.ipcRenderer = electron.ipcRenderer || false
this.state = {
animesTV: this.ipcRenderer.sendSync('get-followedAni') || [],
episodes: false,
followedAnime: false,
torrent: this.ipcRenderer.sendSync('get-downloadedEpi') || {}
}

this.showEpisodes = this.showEpisodes.bind(this)
this.download = this.download.bind(this)
this.playEpisode = this.playEpisode.bind(this)
this.torrentState = this.torrentState.bind(this)
this.reload = this.reload.bind(this)
}

componentDidMount() {
Expand All @@ -33,9 +34,9 @@ export default class extends Component {
torrent[arg.key] = arg
if (arg.progress === 1) {
return {
episodes: this.ipcRenderer
followedAnime: this.ipcRenderer
.sendSync('get-aniList')
.filter(val => val.mal_id === prev.episodes.mal_id)[0],
.filter(val => val.mal_id === prev.followedAnime.mal_id)[0],
torrent
}
}
Expand All @@ -48,14 +49,18 @@ export default class extends Component {

showEpisodes(anime) {
this.setState({
episodes: anime
followedAnime: anime
? this.ipcRenderer
.sendSync('get-aniList')
.filter(val => val.mal_id === anime.mal_id)[0]
: false
})
}

reload(anime) {
this.ipcRenderer.send('reload-episodes', anime)
}

download(obj) {
this.ipcRenderer.send('start-download', obj)
}
Expand All @@ -71,7 +76,7 @@ export default class extends Component {
return (
<Layout>
<div>
{this.state.episodes ? (
{this.state.followedAnime ? (
<div className="episodes">
<div
className="exit"
Expand All @@ -80,54 +85,50 @@ export default class extends Component {
}}
/>
<div className="grid">
{this.state.episodes.episodesHash.map(val => {
{this.state.followedAnime.episodes.map(val => {
return (
<div
className="episode"
key={val.hash.magnet + val.episode}
>
<div className="episode" key={val.magnet + val.number}>
<div className="progress">
<div
className="progress-bar"
style={{
width: this.state.torrent[val.hash.magnet]
? this.state.torrent[val.hash.magnet].progress *
100 +
width: this.state.torrent[val.magnet]
? this.state.torrent[val.magnet].progress * 100 +
'%'
: 0,
backgroundColor:
this.state.torrent[val.hash.magnet] &&
this.state.torrent[val.hash.magnet].progress === 1
this.state.torrent[val.magnet] &&
this.state.torrent[val.magnet].progress === 1
? '#95ff95'
: '#5555ff'
}}
/>
{this.state.torrent[val.hash.magnet] &&
this.state.torrent[val.hash.magnet].progress < 1 ? (
{this.state.torrent[val.magnet] &&
this.state.torrent[val.magnet].progress < 1 ? (
<div className="download_speed">
<span>
{bytesConverter(
this.state.torrent[val.hash.magnet].downloaded
this.state.torrent[val.magnet].downloaded
)}
</span>
<span>
{bytesConverter(
this.state.torrent[val.hash.magnet].speed
this.state.torrent[val.magnet].speed
)}
</span>
</div>
) : null}
</div>
{this.state.torrent[val.hash.magnet] ? (
{this.state.torrent[val.magnet] ? (
<video
src={val.pathname}
src={val.pathnames[0]}
onClick={e => this.playEpisode(e)}
/>
) : (
<button
onClick={() =>
this.download({
anime: this.state.episodes,
anime: this.state.followedAnime,
episode: val
})
}
Expand All @@ -136,7 +137,7 @@ export default class extends Component {
</button>
)}
<div className="title">
{val.hash.title.replace(/\[.*?\]/g, '')}
{val.title.replace(/\[.*?\]/g, '')}
</div>
</div>
)
Expand All @@ -149,6 +150,7 @@ export default class extends Component {
return (
<CadreEpisodes
showEpisodes={this.showEpisodes}
reload={this.reload}
anime={val}
key={val.mal_id}
/>
Expand Down

0 comments on commit c9aab05

Please sign in to comment.