Skip to content

Commit c31cdca

Browse files
committed
Prefer PINE_OUTPUT_DIR or /mnt/pinescripts as default output across scripts
1 parent e4d71af commit c31cdca

4 files changed

Lines changed: 85 additions & 12 deletions

File tree

batch_download.py

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,31 @@
1212
import argparse
1313
import asyncio
1414
import sys
15+
import os
1516
from pathlib import Path
1617

1718
# Import from the enhanced downloader (which is now fixed)
1819
from tv_downloader_enhanced import EnhancedTVScraper
1920

2021

21-
async def batch_download(urls: list[str], output_dir: str = "./pinescript_downloads",
22+
async def batch_download(urls: list[str], output_dir: str | None = None,
2223
delay: float = 2.0, max_pages: int = 10, visible: bool = False, debug_pages: bool = False):
2324
"""Download from multiple URLs sequentially.
2425
26+
visible: show browser window when downloading each page
27+
debug_pages: enable per-page debugging output
28+
"""
29+
# Resolve output_dir default if None
30+
if not output_dir:
31+
env_output = os.environ.get('PINE_OUTPUT_DIR')
32+
if env_output:
33+
output_dir = env_output
34+
elif os.path.exists('/mnt/pinescripts'):
35+
output_dir = '/mnt/pinescripts'
36+
else:
37+
output_dir = './pinescript_downloads'
38+
"""Download from multiple URLs sequentially.
39+
2540
visible: show browser window when downloading each page
2641
debug_pages: enable per-page debugging output
2742
"""
@@ -105,10 +120,19 @@ async def main():
105120
help='URLs to download from'
106121
)
107122

123+
# Default output: prefer env PINE_OUTPUT_DIR, else use /mnt/pinescripts if present, else local folder
124+
env_output = os.environ.get('PINE_OUTPUT_DIR')
125+
if env_output:
126+
default_output = env_output
127+
elif os.path.exists('/mnt/pinescripts'):
128+
default_output = '/mnt/pinescripts'
129+
else:
130+
default_output = './pinescript_downloads'
131+
108132
parser.add_argument(
109133
'--output', '-o',
110-
default='./pinescript_downloads',
111-
help='Output directory'
134+
default=default_output,
135+
help='Output directory (defaults to PINE_OUTPUT_DIR or /mnt/pinescripts when available)'
112136
)
113137

114138
parser.add_argument(

tv_downloader_enhanced.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,19 @@ def extract_script_id(url: str) -> str:
5151

5252

5353
class EnhancedTVScraper:
54-
def __init__(self, output_dir: str = "./pinescript_downloads", headless: bool = True):
55-
self.output_dir = Path(output_dir)
54+
def __init__(self, output_dir: str | None = None, headless: bool = True):
55+
# Resolve default output: prefer env PINE_OUTPUT_DIR, then /mnt/pinescripts, otherwise ./pinescript_downloads
56+
if output_dir:
57+
resolved = output_dir
58+
else:
59+
env_output = os.environ.get('PINE_OUTPUT_DIR')
60+
if env_output:
61+
resolved = env_output
62+
elif os.path.exists('/mnt/pinescripts'):
63+
resolved = '/mnt/pinescripts'
64+
else:
65+
resolved = './pinescript_downloads'
66+
self.output_dir = Path(resolved)
5667
self.headless = headless
5768
self.browser = None
5869
self.context = None

tv_downloader_fixed.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,18 @@ def extract_script_id(url: str) -> str:
3939

4040

4141
class TVPineScriptDownloader:
42-
def __init__(self, output_dir: str = "./pinescript_downloads", headless: bool = True):
43-
self.output_dir = Path(output_dir)
42+
def __init__(self, output_dir: str | None = None, headless: bool = True):
43+
if output_dir:
44+
resolved = output_dir
45+
else:
46+
env_output = os.environ.get('PINE_OUTPUT_DIR')
47+
if env_output:
48+
resolved = env_output
49+
elif os.path.exists('/mnt/pinescripts'):
50+
resolved = '/mnt/pinescripts'
51+
else:
52+
resolved = './pinescript_downloads'
53+
self.output_dir = Path(resolved)
4454
self.headless = headless
4555
self.browser = None
4656
self.context = None
@@ -386,7 +396,16 @@ def _print_summary(self, category: str):
386396
async def main():
387397
parser = argparse.ArgumentParser(description='Download Pine Scripts from TradingView')
388398
parser.add_argument('--url', '-u', required=True, help='TradingView scripts URL')
389-
parser.add_argument('--output', '-o', default='./pinescript_downloads', help='Output directory')
399+
# Default output: prefer env PINE_OUTPUT_DIR, else /mnt/pinescripts if present, else local folder
400+
env_output = os.environ.get('PINE_OUTPUT_DIR')
401+
if env_output:
402+
default_output = env_output
403+
elif os.path.exists('/mnt/pinescripts'):
404+
default_output = '/mnt/pinescripts'
405+
else:
406+
default_output = './pinescript_downloads'
407+
408+
parser.add_argument('--output', '-o', default=default_output, help='Output directory')
390409
parser.add_argument('--max-pages', '-p', type=int, default=30, help='Max show-more clicks')
391410
parser.add_argument('--delay', '-d', type=float, default=2.0, help='Delay between downloads')
392411
parser.add_argument('--visible', action='store_true', help='Show browser window')

tv_pinescript_downloader.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,18 @@ def extract_script_name(url: str) -> str:
5555

5656

5757
class TradingViewScraper:
58-
def __init__(self, output_dir: str = "./pinescript_downloads", headless: bool = True):
59-
self.output_dir = Path(output_dir)
58+
def __init__(self, output_dir: str | None = None, headless: bool = True):
59+
if output_dir:
60+
resolved = output_dir
61+
else:
62+
env_output = os.environ.get('PINE_OUTPUT_DIR')
63+
if env_output:
64+
resolved = env_output
65+
elif os.path.exists('/mnt/pinescripts'):
66+
resolved = '/mnt/pinescripts'
67+
else:
68+
resolved = './pinescript_downloads'
69+
self.output_dir = Path(resolved)
6070
self.headless = headless
6171
self.browser = None
6272
self.context = None
@@ -425,8 +435,17 @@ async def main():
425435

426436
parser.add_argument(
427437
'--output', '-o',
428-
default='./pinescript_downloads',
429-
help='Output directory for downloaded scripts (default: ./pinescript_downloads)'
438+
# Default output: prefer env PINE_OUTPUT_DIR, else /mnt/pinescripts if present, else local folder
439+
env_output = os.environ.get('PINE_OUTPUT_DIR')
440+
if env_output:
441+
default_output = env_output
442+
elif os.path.exists('/mnt/pinescripts'):
443+
default_output = '/mnt/pinescripts'
444+
else:
445+
default_output = './pinescript_downloads'
446+
447+
default=default_output,
448+
help='Output directory for downloaded scripts (default: PINE_OUTPUT_DIR or /mnt/pinescripts if available)'
430449
)
431450

432451
parser.add_argument(

0 commit comments

Comments
 (0)