forked from platypush/platypush
Support for multiple torrent sessions
This commit is contained in:
parent
26f6feebb7
commit
80d5a6ad19
1 changed files with 33 additions and 47 deletions
|
@ -1,12 +1,9 @@
|
||||||
import json
|
|
||||||
import os
|
import os
|
||||||
import random
|
import random
|
||||||
|
import requests
|
||||||
import threading
|
import threading
|
||||||
import time
|
import time
|
||||||
|
|
||||||
import urllib.request
|
|
||||||
import urllib.parse
|
|
||||||
|
|
||||||
from platypush.context import get_bus
|
from platypush.context import get_bus
|
||||||
from platypush.plugins import Plugin, action
|
from platypush.plugins import Plugin, action
|
||||||
from platypush.message.event.torrent import \
|
from platypush.message.event.torrent import \
|
||||||
|
@ -60,7 +57,7 @@ class TorrentPlugin(Plugin):
|
||||||
|
|
||||||
self.torrent_ports = torrent_ports if torrent_ports else self.default_torrent_ports
|
self.torrent_ports = torrent_ports if torrent_ports else self.default_torrent_ports
|
||||||
self.download_dir = None
|
self.download_dir = None
|
||||||
self._session = None
|
self._sessions = {}
|
||||||
|
|
||||||
if download_dir:
|
if download_dir:
|
||||||
self.download_dir = os.path.abspath(os.path.expanduser(download_dir))
|
self.download_dir = os.path.abspath(os.path.expanduser(download_dir))
|
||||||
|
@ -116,14 +113,12 @@ class TorrentPlugin(Plugin):
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
def search_movies(self, query, language=None):
|
def search_movies(self, query, language=None):
|
||||||
request = urllib.request.urlopen(urllib.request.Request(
|
response = requests.get(
|
||||||
'https://movies-v2.api-fetch.sh/movies/1?' + urllib.parse.urlencode({
|
'https://movies-v2.api-fetch.sh/movies/1', params={
|
||||||
'sort': 'relevance',
|
'sort': 'relevance',
|
||||||
'keywords': query,
|
'keywords': query,
|
||||||
}), headers=self.headers
|
}, headers=self.headers
|
||||||
))
|
).json()
|
||||||
|
|
||||||
response = json.loads(request.read().decode('utf-8'))
|
|
||||||
|
|
||||||
return sorted([
|
return sorted([
|
||||||
{
|
{
|
||||||
|
@ -148,22 +143,20 @@ class TorrentPlugin(Plugin):
|
||||||
], key=lambda _: _.get('seeds'), reverse=True)
|
], key=lambda _: _.get('seeds'), reverse=True)
|
||||||
|
|
||||||
def search_tv(self, query):
|
def search_tv(self, query):
|
||||||
request = urllib.request.urlopen(urllib.request.Request(
|
shows = requests.get(
|
||||||
'https://tv-v2.api-fetch.sh/shows/1?' + urllib.parse.urlencode({
|
'https://tv-v2.api-fetch.sh/shows/1', params={
|
||||||
'sort': 'relevance',
|
'sort': 'relevance',
|
||||||
'keywords': query,
|
'keywords': query,
|
||||||
}), headers=self.headers
|
}, headers=self.headers
|
||||||
))
|
).json()
|
||||||
|
|
||||||
results = []
|
results = []
|
||||||
shows = json.loads(request.read().decode('utf-8'))
|
|
||||||
|
|
||||||
for show in shows:
|
for show in shows:
|
||||||
request = urllib.request.urlopen(urllib.request.Request(
|
show = requests.get(
|
||||||
'https://tv-v2.api-fetch.website/show/' + show.get('_id'),
|
'https://tv-v2.api-fetch.website/show/' + show.get('_id'),
|
||||||
headers=self.headers))
|
headers=self.headers
|
||||||
|
).json()
|
||||||
show = json.loads(request.read().decode('utf-8'))
|
|
||||||
|
|
||||||
results.extend(sorted([
|
results.extend(sorted([
|
||||||
{
|
{
|
||||||
|
@ -197,22 +190,18 @@ class TorrentPlugin(Plugin):
|
||||||
return results
|
return results
|
||||||
|
|
||||||
def search_anime(self, query):
|
def search_anime(self, query):
|
||||||
request = urllib.request.urlopen(urllib.request.Request(
|
shows = requests.get(
|
||||||
'https://popcorn-api.io/animes/1?' + urllib.parse.urlencode({
|
'https://popcorn-api.io/animes/1', params={
|
||||||
'sort': 'relevance',
|
'sort': 'relevance',
|
||||||
'keywords': query,
|
'keywords': query,
|
||||||
}), headers=self.headers
|
}, headers=self.headers
|
||||||
))
|
).json()
|
||||||
|
|
||||||
results = []
|
results = []
|
||||||
shows = json.loads(request.read().decode('utf-8'))
|
|
||||||
|
|
||||||
for show in shows:
|
for show in shows:
|
||||||
request = urllib.request.urlopen(urllib.request.Request(
|
show = requests.get('https://anime.api-fetch.website/anime/' + show.get('_id'),
|
||||||
'https://anime.api-fetch.website/anime/' + show.get('_id'),
|
headers=self.headers).json()
|
||||||
headers=self.headers))
|
|
||||||
|
|
||||||
show = json.loads(request.read().decode('utf-8'))
|
|
||||||
|
|
||||||
results.extend(sorted([
|
results.extend(sorted([
|
||||||
{
|
{
|
||||||
|
@ -254,10 +243,8 @@ class TorrentPlugin(Plugin):
|
||||||
info = lt.parse_magnet_uri(magnet)
|
info = lt.parse_magnet_uri(magnet)
|
||||||
info['magnet'] = magnet
|
info['magnet'] = magnet
|
||||||
elif torrent.startswith('http://') or torrent.startswith('https://'):
|
elif torrent.startswith('http://') or torrent.startswith('https://'):
|
||||||
import requests
|
response = requests.get(torrent, headers=self.headers, allow_redirects=True)
|
||||||
response = requests.get(torrent, allow_redirects=True)
|
torrent_file = os.path.join(download_dir, self._generate_rand_filename())
|
||||||
torrent_file = os.path.join(download_dir,
|
|
||||||
self._generate_rand_filename())
|
|
||||||
|
|
||||||
with open(torrent_file, 'wb') as f:
|
with open(torrent_file, 'wb') as f:
|
||||||
f.write(response.content)
|
f.write(response.content)
|
||||||
|
@ -405,10 +392,14 @@ class TorrentPlugin(Plugin):
|
||||||
os.makedirs(download_dir, exist_ok=True)
|
os.makedirs(download_dir, exist_ok=True)
|
||||||
info, file_info, torrent_file, magnet = self._get_torrent_info(torrent, download_dir)
|
info, file_info, torrent_file, magnet = self._get_torrent_info(torrent, download_dir)
|
||||||
|
|
||||||
if not self._session:
|
if torrent in self._sessions:
|
||||||
# noinspection PyArgumentList
|
self.logger.info('A torrent session is already running for {}'.format(torrent))
|
||||||
self._session = lt.session()
|
return self.torrent_state.get(torrent, {})
|
||||||
self._session.listen_on(*self.torrent_ports)
|
|
||||||
|
# noinspection PyArgumentList
|
||||||
|
session = lt.session()
|
||||||
|
session.listen_on(*self.torrent_ports)
|
||||||
|
self._sessions[torrent] = session
|
||||||
|
|
||||||
params = {
|
params = {
|
||||||
'save_path': download_dir,
|
'save_path': download_dir,
|
||||||
|
@ -416,10 +407,10 @@ class TorrentPlugin(Plugin):
|
||||||
}
|
}
|
||||||
|
|
||||||
if magnet:
|
if magnet:
|
||||||
transfer = lt.add_magnet_uri(self._session, magnet, params)
|
transfer = lt.add_magnet_uri(session, magnet, params)
|
||||||
else:
|
else:
|
||||||
params['ti'] = file_info
|
params['ti'] = file_info
|
||||||
transfer = self._session.add_torrent(params)
|
transfer = session.add_torrent(params)
|
||||||
|
|
||||||
self.transfers[torrent] = transfer
|
self.transfers[torrent] = transfer
|
||||||
self.torrent_state[torrent] = {
|
self.torrent_state[torrent] = {
|
||||||
|
@ -505,19 +496,14 @@ class TorrentPlugin(Plugin):
|
||||||
del self.torrent_state[torrent]
|
del self.torrent_state[torrent]
|
||||||
del self.transfers[torrent]
|
del self.transfers[torrent]
|
||||||
|
|
||||||
if not len(self.transfers):
|
if torrent in self._sessions:
|
||||||
self.logger.info('No remaining active torrent transfers found, exiting session')
|
del self._sessions[torrent]
|
||||||
self._session = None
|
|
||||||
|
|
||||||
@action
|
@action
|
||||||
def quit(self):
|
def quit(self):
|
||||||
"""
|
"""
|
||||||
Quits all the transfers and the active session
|
Quits all the transfers and the active session
|
||||||
"""
|
"""
|
||||||
if not self._session:
|
|
||||||
self.logger.info('No active sessions found')
|
|
||||||
return
|
|
||||||
|
|
||||||
transfers = self.transfers.copy()
|
transfers = self.transfers.copy()
|
||||||
for torrent in transfers:
|
for torrent in transfers:
|
||||||
self.remove(torrent)
|
self.remove(torrent)
|
||||||
|
|
Loading…
Reference in a new issue