Skip to content

Commit

Permalink
Merge pull request #5 from dzhuang/allow_parse_url
Browse files Browse the repository at this point in the history
Allow parse subscription url with http:// or https://.
  • Loading branch information
dzhuang authored May 12, 2024
2 parents d3b8aa7 + 3682ed8 commit 3f9f604
Showing 1 changed file with 30 additions and 4 deletions.
34 changes: 30 additions & 4 deletions src/singbox_converter/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,21 @@ def get_content_from_sub(self, subscribe, max_retries=6):
)
n_retries += 1

def get_content_from_url(self, subscribe):
url = subscribe["url"]
user_agent = subscribe.get('User-Agent', self.fetch_sub_ua)
response = self.session.get(
url, headers={"User-Agent": user_agent}
)
if response.status_code != 200:
raise FailedToFetchSubscription()

from ruamel.yaml import YAML

yaml = YAML(typ="safe", pure=True)
yaml_data = dict(yaml.load(response.text))
return yaml_data

def get_nodes_from_sub(self, subscribe):
url_or_path = subscribe["url"]

Expand All @@ -413,10 +428,21 @@ def get_nodes_from_sub(self, subscribe):
if url_or_path.startswith('sub://'):
url_or_path = b64_decode(url_or_path[6:]).decode('utf-8')

try:
_content = self.get_content_from_file(url_or_path)
except Exception:
pass
if os.path.exists(url_or_path):
try:
_content = self.get_content_from_file(url_or_path)
except Exception as e:
self.logger.warning(
f"Failed to load '{url_or_path}' as a subscription file: "
f"{type(e).__name__}: {str(e)}")
else:
if url_or_path.startswith(('http://', 'https://')):
try:
_content = self.get_content_from_url(subscribe)
except Exception as e:
self.logger.warning(
f"Failed to load '{url_or_path}' as a subscription url: "
f"{type(e).__name__}: {str(e)}")

if _content is None:
url_str = urlparse(url_or_path)
Expand Down

0 comments on commit 3f9f604

Please sign in to comment.