From 7a132278f8bc0263f521a124caf0593c8ba89039 Mon Sep 17 00:00:00 2001 From: Jimmy Situ Date: Tue, 20 Aug 2024 21:09:34 +0800 Subject: [PATCH] Update tempdir, using tempfile --- .github/workflows/python-package.yml | 2 +- examples/get_xshg.py | 2 +- msfinance/stocks.py | 9 +++++++-- pyproject.toml | 1 + requirements.txt | 1 + 5 files changed, 11 insertions(+), 4 deletions(-) diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-package.yml index 760c8f8..fefeafc 100644 --- a/.github/workflows/python-package.yml +++ b/.github/workflows/python-package.yml @@ -32,7 +32,7 @@ jobs: run: | python -m pip install --upgrade pip python -m pip install flake8 pytest - if [ -f requirements.txt ]; then pip install -r requirements.txt; fi + pip install -r requirements.txt - name: Lint with flake8 run: | # stop the build if there are Python syntax errors or undefined names diff --git a/examples/get_xshg.py b/examples/get_xshg.py index 7652b3d..0cec223 100755 --- a/examples/get_xshg.py +++ b/examples/get_xshg.py @@ -5,7 +5,7 @@ proxy = 'socks5://127.0.0.1:1088' stock = msf.Stock( - debug=True, + debug=True, session='xshg.db3', proxy=proxy, ) diff --git a/msfinance/stocks.py b/msfinance/stocks.py index 2e279de..b8b1c58 100644 --- a/msfinance/stocks.py +++ b/msfinance/stocks.py @@ -5,6 +5,7 @@ import json import sqlite3 import requests +import tempfile import pandas as pd @@ -39,7 +40,7 @@ } class StockBase: - def __init__(self, debug=False, browser='firefox', session='/tmp/msfinance/msfinance.db', proxy=None): + def __init__(self, debug=False, browser='firefox', session='msfinance.db3', proxy=None): self.debug = debug if('chrome' == browser): # TODO: Add chrome support @@ -49,7 +50,11 @@ def __init__(self, debug=False, browser='firefox', session='/tmp/msfinance/msfin self.options = webdriver.FirefoxOptions() # Settting download staff - self.download_dir = '/tmp/msfinance/' + str(os.getpid()) + self.download_dir = os.path.join(tempfile.gettempdir(), 'msfinance', str(os.getpid())) + + if not os.path.exists(self.download_dir): + os.makedirs(self.download_dir) + self.options.set_preference("browser.download.folderList", 2) self.options.set_preference("browser.download.dir", self.download_dir) self.options.set_preference("browser.download.useDownloadDir", True) diff --git a/pyproject.toml b/pyproject.toml index ce1dc59..f220c44 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -20,6 +20,7 @@ dependencies = [ "retrying", "xlrd", "lxml", + "tempfile", "requests", "pandas", "selenium", diff --git a/requirements.txt b/requirements.txt index c4fe3ad..1b0ff0b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,7 @@ retrying xlrd lxml +tempfile requests pandas selenium