2020-03-21 04:41:33 +01:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
2020-03-20 03:34:36 +01:00
|
|
|
################################################################################
|
|
|
|
# Author: Poka (poka@p2p.legal)
|
2020-03-22 02:37:25 +01:00
|
|
|
# Version: 0.1.0
|
2020-03-20 03:34:36 +01:00
|
|
|
# License: AGPL-3.0 (https://choosealicense.com/licenses/agpl-3.0/)
|
|
|
|
# Git: https://git.p2p.legal/axiom-team/astroport
|
|
|
|
################################################################################
|
|
|
|
|
2020-03-21 04:41:33 +01:00
|
|
|
# Config
|
2020-03-22 02:37:25 +01:00
|
|
|
diskLimit = 15
|
2020-03-21 04:41:33 +01:00
|
|
|
tkdelay = 3
|
|
|
|
displayDetails = "true"
|
|
|
|
|
2020-03-20 02:00:21 +01:00
|
|
|
import requests
|
|
|
|
import json
|
|
|
|
import sys
|
2020-03-21 04:41:33 +01:00
|
|
|
import os
|
2020-08-09 20:24:38 +02:00
|
|
|
import shutil
|
2020-03-20 07:51:25 +01:00
|
|
|
import subprocess
|
2020-03-20 02:00:21 +01:00
|
|
|
import login
|
2020-03-20 07:51:25 +01:00
|
|
|
import time
|
2020-03-21 04:41:33 +01:00
|
|
|
import re
|
2020-03-22 02:37:25 +01:00
|
|
|
from termcolor import colored
|
2020-08-30 17:58:50 +02:00
|
|
|
from optparse import OptionParser
|
2020-09-03 19:33:00 +02:00
|
|
|
from urllib.parse import unquote
|
2020-08-30 17:58:50 +02:00
|
|
|
|
|
|
|
# Load options
|
|
|
|
parser = OptionParser()
|
|
|
|
parser.add_option("-s", "--seed", action="store_false", dest="rmTracker", default=True,
|
|
|
|
help="Keep the tracker for this torrent. So ratio is supported.")
|
|
|
|
parser.add_option("-q", "--quiet",
|
|
|
|
action="store_false", dest="verbose", default=True,
|
|
|
|
help="don't print status messages to stdout")
|
|
|
|
(options, args) = parser.parse_args()
|
|
|
|
|
2020-03-20 02:00:21 +01:00
|
|
|
|
2020-03-20 03:34:36 +01:00
|
|
|
# Load scraper
|
2020-03-20 02:44:22 +01:00
|
|
|
from yggcrawl import YggTorrentScraper
|
2020-03-20 02:00:21 +01:00
|
|
|
scraper = YggTorrentScraper(requests.session())
|
|
|
|
from yggtorrentscraper import set_yggtorrent_tld
|
2020-08-11 02:21:00 +02:00
|
|
|
set_yggtorrent_tld("si")
|
2020-03-20 07:51:25 +01:00
|
|
|
name = ' '.join(sys.argv[1:])
|
2020-03-20 02:00:21 +01:00
|
|
|
|
2020-03-22 06:35:11 +01:00
|
|
|
# Search torrent name
|
2020-09-03 19:33:00 +02:00
|
|
|
if ("https://" not in name):
|
|
|
|
name = re.sub(r'\w*-\w*', '', name)
|
|
|
|
research = os.popen('./lib/scrabash.sh search --best=true ' + name).read()
|
2020-03-21 04:41:33 +01:00
|
|
|
|
2020-09-03 19:33:00 +02:00
|
|
|
try:
|
|
|
|
research.index("No torrent found")
|
|
|
|
except ValueError:
|
|
|
|
True
|
|
|
|
else:
|
|
|
|
print(colored('No torrent named "' + name + '" on YggTorrent', 'blue'))
|
|
|
|
sys.exit(1)
|
2020-03-21 04:41:33 +01:00
|
|
|
else:
|
2020-09-03 19:33:00 +02:00
|
|
|
research = unquote(name, errors='strict')
|
2020-03-20 02:00:21 +01:00
|
|
|
|
2020-08-09 20:24:38 +02:00
|
|
|
# Allow only one torrent downling in same time, and remove oldest torrent if disk size is full.
|
2020-03-22 02:37:25 +01:00
|
|
|
def rollingFiles():
|
|
|
|
def isDL():
|
|
|
|
downloading = os.popen('./trans-ctl.sh downloading').read()
|
|
|
|
if "There is a torrent downloading" in downloading:
|
|
|
|
print("A torrent is downloading, waiting 1 minute")
|
|
|
|
time.sleep(60)
|
|
|
|
isDL()
|
|
|
|
|
|
|
|
isDL()
|
2020-03-26 07:24:04 +01:00
|
|
|
global idTorrent
|
2020-03-22 02:37:25 +01:00
|
|
|
idTorrent = os.popen('./lib/scrabash.sh get_details ' + research).read()
|
2020-03-23 05:24:14 +01:00
|
|
|
dbPath = "./data/meta/{}/db.py".format(idTorrent.rstrip())
|
2020-03-22 02:37:25 +01:00
|
|
|
torrentDB = os.popen('cat ' + dbPath).read()
|
2020-03-23 06:49:34 +01:00
|
|
|
print(colored(torrentDB, 'blue'))
|
2020-03-22 02:37:25 +01:00
|
|
|
print("---")
|
|
|
|
|
|
|
|
torrentDBFile = open(dbPath, "r")
|
|
|
|
|
|
|
|
for line in torrentDBFile:
|
|
|
|
if re.search("Size", line):
|
|
|
|
fields = line.strip().split()
|
|
|
|
if "M" in fields[2]:
|
|
|
|
torrentSize = 1
|
|
|
|
else:
|
|
|
|
torrentSize = float(fields[2]) #kopa
|
|
|
|
|
|
|
|
# Check space on disk
|
|
|
|
def checkDisk():
|
|
|
|
global diskSize
|
|
|
|
diskSize = os.popen('./trans-ctl.sh checkdisk').read()
|
2020-03-22 06:35:11 +01:00
|
|
|
if re.search("You have reached", diskSize):
|
|
|
|
fields = diskSize.strip().split()
|
|
|
|
if "M" in diskSize:
|
|
|
|
diskSize = float(fields[-1])
|
|
|
|
|
2020-03-22 02:37:25 +01:00
|
|
|
diskEnd = float(diskSize) + torrentSize
|
|
|
|
if torrentSize >= diskLimit:
|
|
|
|
print(colored('This torrent is larger as your disk limit:', 'red'))
|
|
|
|
print("Disk usage: " + str(diskSize.rstrip()))
|
|
|
|
print("Limit: " + str(diskLimit))
|
|
|
|
print("Size of this torrent: " + str(colored(torrentSize, 'red')))
|
|
|
|
sys.exit(colored("Torrent too large", 'red'))
|
|
|
|
|
|
|
|
if diskEnd >= diskLimit:
|
|
|
|
print(colored("You have filled the entire space allocated to torrents:", 'red'))
|
|
|
|
print("Disk usage: " + str(diskSize.rstrip()))
|
|
|
|
print("Limit: " + str(diskLimit))
|
|
|
|
print("With this torrent: " + str(colored(diskEnd, 'red')))
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
print(colored('Remove oldest torrent...', 'yellow'))
|
|
|
|
lowerid = os.popen('./trans-ctl.sh getlowerid').read()
|
|
|
|
os.popen('./trans-ctl.sh remove ' + lowerid)
|
|
|
|
time.sleep(3)
|
|
|
|
checkDisk()
|
|
|
|
# sys.exit(colored("Disk full", 'red'))
|
|
|
|
|
|
|
|
checkDisk()
|
2020-03-26 17:36:29 +01:00
|
|
|
|
2020-03-22 02:37:25 +01:00
|
|
|
# Download Torrent
|
|
|
|
def downloadTorrent():
|
|
|
|
# Download torrent file
|
2020-08-30 17:58:50 +02:00
|
|
|
if len(os.listdir('data/tmp/torrents') ) != 0:
|
|
|
|
shutil.rmtree('data/tmp/torrents', ignore_errors=True)
|
|
|
|
os.mkdir("data/tmp/torrents")
|
|
|
|
scraper.download_from_torrent_url(research)
|
2020-03-26 07:24:04 +01:00
|
|
|
# os.popen(f'cd data/tmp/torrents/ && mv *.torrent {idTorrent}.torrent && mv *.torrent ../../torrents/')
|
2020-08-30 17:58:50 +02:00
|
|
|
os.popen(f'cd data/tmp/torrents/ && mv *.torrent {idTorrent.strip()}.torrent && mv {idTorrent.strip()}.torrent ../../torrents/')
|
2020-03-20 07:51:25 +01:00
|
|
|
|
|
|
|
# Remove tracker
|
2020-03-22 02:37:25 +01:00
|
|
|
def removeTracker():
|
|
|
|
print(colored("Start to remove tracker ...", 'yellow'))
|
|
|
|
higherid = os.popen('./trans-ctl.sh gethigherid').read()
|
|
|
|
time.sleep(1)
|
|
|
|
print("Wait " + str(tkdelay) + " seconds to get peers before remove tracker...")
|
|
|
|
time.sleep(tkdelay)
|
2020-03-26 07:24:04 +01:00
|
|
|
os.popen('./trans-ctl.sh rmtracker ' + name)
|
|
|
|
os.popen('./trans-ctl.sh rmtracker ' + higherid)
|
2020-08-30 17:58:50 +02:00
|
|
|
os.replace(f'data/torrents/{idTorrent.strip()}.torrent.added', f'data/meta/{idTorrent.strip()}/{idTorrent.strip()}.torrent')
|
|
|
|
|
|
|
|
|
|
|
|
if(scraper.login(login.user, login.passwd)): #Check if user can login
|
|
|
|
print(colored("Login success", 'green'))
|
|
|
|
rollingFiles()
|
|
|
|
downloadTorrent()
|
|
|
|
removeTracker() if options.rmTracker else None
|
|
|
|
else:
|
|
|
|
print(colored("Login failed", 'red'))
|
|
|
|
sys.exit(1)
|
2020-03-22 02:37:25 +01:00
|
|
|
|
|
|
|
|
2020-03-22 06:35:11 +01:00
|
|
|
# End
|
2020-03-22 02:37:25 +01:00
|
|
|
print(colored("Done", 'green'))
|
2020-03-22 06:35:11 +01:00
|
|
|
sys.exit(0)
|