Skip to content

Commit

Permalink
Several connection attempts (certain tracker are very unstable).
Browse files Browse the repository at this point in the history
Replacing the "size" with "total_size" in "fingerprint".
Fix error of reading a missing parameter in the settings.
Fix #1 issue.
  • Loading branch information
konkere committed Oct 13, 2024
1 parent 3ecc020 commit 985ed88
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 50 deletions.
7 changes: 5 additions & 2 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import re
from client import QBT, TM
from os import path, getenv, mkdir
from configparser import ConfigParser
from configparser import ConfigParser, NoOptionError, NoSectionError


def get_ids_from_file(update_file, tracker_ids):
Expand Down Expand Up @@ -146,7 +146,10 @@ def create_update_file(self):
raise FileNotFoundError(f'Required to fill list of topics id in: {self.update_file}')

def read_config(self, section, setting):
value = self.config.get(section, setting)
try:
value = self.config.get(section, setting)
except (NoSectionError, NoOptionError):
value = ''
return value

def get_ids(self):
Expand Down
95 changes: 57 additions & 38 deletions torrent_updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,60 @@
import logging
import requests
from config import Conf
from bencoder import bdecode
from bencoder import bdecode, BTFailure
from urllib.parse import urljoin
from tracker import RuTracker, NNMClub, TeamHD, Kinozal, rss_parser


def get_torrent(tracker):
torrent = None
torrent_name = None
for _ in range(5):
torrent = tracker.download_torrent()
try:
torrent_name = bdecode(torrent)[b'info'][b'name'].decode('UTF-8')
except BTFailure:
torrent = None
else:
break
return torrent_name, torrent


def run_through_tracker(config, sessions, tracker, trackers):
for topic_id in config.tracker_ids[tracker]:
current_torrent = config.client.get_torrent_by_topic(tracker, topic_id)
fresh_tracker = trackers[tracker]['incarnation'](
auth=config.auth[tracker],
topic_id=topic_id,
session=sessions[tracker]
)
current_fingerprint = str(current_torrent[trackers[tracker]['fingerprint']]).lower()
if current_fingerprint != fresh_tracker.fingerprint.lower() and fresh_tracker.fingerprint:
new_torrent_name, new_torrent = get_torrent(fresh_tracker)
if new_torrent_name and new_torrent:
if not sessions[tracker] and fresh_tracker.session:
sessions[tracker] = fresh_tracker.session
data = {
'category': current_torrent['category'],
'tags': current_torrent['tags'],
'path': current_torrent['save_path'],
'state': current_torrent['state'],
'tracker': tracker,
'topic_id': topic_id,
}
config.client.remove_torrent(current_torrent['hash'])
logging.info(
f'The torrent {trackers[tracker]["fingerprint"]} in topic {fresh_tracker.topic_url} has changed. '
f'Updating the torrent — {current_torrent["name"]}'
)
config.client.add_torrent(torrent=new_torrent, data=data)
if new_torrent_name != current_torrent['name']:
logging.warning(
f'The torrent name has changed: {current_torrent["name"]}{new_torrent_name}. '
f'Duplicate files may appear.'
)


def main():
config = Conf()
sessions = {}
Expand All @@ -25,13 +74,13 @@ def main():
},
'teamhd': {
'incarnation': TeamHD,
'fingerprint': 'size',
'fingerprint': 'total_size',
'rssjoin': 'rss.php?feed=dl&passkey=',
'dl_from': 'feed',
},
'kinozal': {
'incarnation': Kinozal,
'fingerprint': 'size',
'fingerprint': 'total_size',
'dl_from': 'topic',
},
}
Expand All @@ -48,41 +97,11 @@ def main():
rss_url = f'{rss_url}{config.auth[tracker]["passkey"]}'
config.tracker_ids[tracker] = rss_parser(rss_url, config.tracker_ids[tracker])
for tracker in config.tracker_ids.keys():
response = requests.get(config.auth[tracker]['url'])
tracker_status = response.status_code
if tracker_status == 200:
for topic_id in config.tracker_ids[tracker]:
current_torrent = config.client.get_torrent_by_topic(tracker, topic_id)
fresh_tracker = trackers[tracker]['incarnation'](
auth=config.auth[tracker],
topic_id=topic_id,
session=sessions[tracker]
)
current_fingerprint = str(current_torrent[trackers[tracker]['fingerprint']]).lower()
if current_fingerprint != fresh_tracker.fingerprint.lower() and fresh_tracker.fingerprint:
new_torrent = fresh_tracker.download_torrent()
new_torrent_name = bdecode(new_torrent)[b'info'][b'name'].decode('UTF-8')
if not sessions[tracker] and fresh_tracker.session:
sessions[tracker] = fresh_tracker.session
data = {
'category': current_torrent['category'],
'tags': current_torrent['tags'],
'path': current_torrent['save_path'],
'state': current_torrent['state'],
'tracker': tracker,
'topic_id': topic_id,
}
config.client.remove_torrent(current_torrent['hash'])
logging.info(
f'The torrent {trackers[tracker]["fingerprint"]} in topic {fresh_tracker.topic_url} has changed. '
f'Updating the torrent — {current_torrent["name"]}'
)
config.client.add_torrent(torrent=new_torrent, data=data)
if new_torrent_name != current_torrent['name']:
logging.warning(
f'The torrent name has changed: {current_torrent["name"]}{new_torrent_name}. '
f'Duplicate files may appear.'
)
for _ in range(5):
response = requests.get(config.auth[tracker]['url'])
if response.status_code == 200:
run_through_tracker(config, sessions, tracker, trackers)
break


if __name__ == '__main__':
Expand Down
24 changes: 14 additions & 10 deletions tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,18 @@ def download_torrent(self):
return torrent

def get_actual_weight(self):
response = requests.get(self.topic_url)
topic = BeautifulSoup(response.text, features='html.parser')
try:
weight_field = topic.find('span', {'class': 'floatright green n'}).get_text()
except AttributeError:
weight = ''
else:
pattern = r'^[\s\./d\w]*\(([\d\,]*)\)$'
weight = re.match(pattern, weight_field).group(1)
weight = weight.replace(',', '')
weight = ''
for _ in range(5):
response = requests.get(self.topic_url)
topic = BeautifulSoup(response.text, features='html.parser')
try:
weight_field = topic.find('span', {'class': 'floatright green n'}).get_text()
except AttributeError:
pass
else:
pattern = r'^[\s\./d\w]*\(([\d\,]*)\)$'
weight = re.match(pattern, weight_field).group(1)
weight = weight.replace(',', '')
if weight:
break
return weight

0 comments on commit 985ed88

Please sign in to comment.