astroport-iptubes/crawl.py

134 lines
4.5 KiB
Python
Executable File

#!/usr/bin/python3
################################################################################
# Author: Poka (poka@p2p.legal)
# Version: 0.1.0
# License: AGPL-3.0 (https://choosealicense.com/licenses/agpl-3.0/)
# Git: https://git.p2p.legal/axiom-team/astroport
################################################################################
# Config
diskLimit = 15
tkdelay = 3
displayDetails = "true"
import requests
import json
import sys
import os
import subprocess
import login
import time
import re
from termcolor import colored
# Load scraper
from yggcrawl import YggTorrentScraper
scraper = YggTorrentScraper(requests.session())
from yggtorrentscraper import set_yggtorrent_tld
set_yggtorrent_tld("se")
name = ' '.join(sys.argv[1:])
# Search torrent name
research = os.popen('./lib/scrabash.sh search --best=true ' + name).read()
try:
research.index("No torrent found")
except ValueError:
True
else:
sys.exit(1)
# Rollong Files
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()
global idTorrent
idTorrent = os.popen('./lib/scrabash.sh get_details ' + research).read()
dbPath = "./data/meta/{}/db.py".format(idTorrent.rstrip())
torrentDB = os.popen('cat ' + dbPath).read()
print(colored(torrentDB, 'blue'))
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()
if re.search("You have reached", diskSize):
fields = diskSize.strip().split()
if "M" in diskSize:
diskSize = float(fields[-1])
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()
# Download Torrent
def downloadTorrent():
# Download torrent file
if(scraper.login(login.user, login.passwd)):
print(colored("Login success", 'green'))
subprocess.Popen('[[ $(ls data/tmp/torrents/) ]] && rm data/tmp/torrents/*', executable='/bin/bash')
scraper.download_from_torrent_url(research)
# os.popen(f'cd data/tmp/torrents/ && mv *.torrent {idTorrent}.torrent && mv *.torrent ../../torrents/')
os.popen(f'cd data/tmp/torrents/ && mv *.torrent {idTorrent.strip()}.torrent && mv {idTorrent.strip()}.torrent ../../torrents/')
else:
print(colored("Login failed", 'red'))
sys.exit(1)
# Remove tracker
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)
os.popen('./trans-ctl.sh rmtracker ' + name)
os.popen('./trans-ctl.sh rmtracker ' + higherid)
# print(tkresult)
rollingFiles()
downloadTorrent()
removeTracker()
# End
print(colored("Done", 'green'))
sys.exit(0)