Skip to content

Commit

Permalink
Merge pull request #137 from ancoka/feature_matext_mate70
Browse files Browse the repository at this point in the history
refact(huawei.py): 支持Mate系列等最新机型
  • Loading branch information
ancoka authored Nov 18, 2024
2 parents a453832 + d440d7d commit a52dc2b
Show file tree
Hide file tree
Showing 16 changed files with 300 additions and 257 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ html
**/*.log
hw_cookies.txt
profiles/*
__pycache__

3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@
- [x] 无窗口模式支持
- [x] 多浏览器支持(chrome、firefox、edge、safari)
- [x] 多线程支持
- [ ] 京东APP抢购(规划中)
- [ ] 下单成功通知功能(待实现)
- [x] 支持 Mate XT 非凡大师、Mate 70 系列等最新机型

## 运行环境
请安装大于等于python 3.6 的版本及同浏览器版本匹配的浏览器驱动运行此项目
Expand Down
1 change: 0 additions & 1 deletion browser/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@


class Browser(ABC):

@abstractmethod
def setting(self, config: Config = None, log_path: str = ""):
pass
11 changes: 6 additions & 5 deletions browser/browser_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
from browser.safari import SafariBrowser
from loguru import logger


class BrowserFactory:

@staticmethod
def build(browserType):
supportBrowsers = ['chrome', 'firefox', 'edge', 'safari']
if browserType not in supportBrowsers:
logger.info("不支持的浏览器类型,浏览器类型为:{}", browserType)
def build(browser_type):
support_browsers = ['chrome', 'firefox', 'edge', 'safari']
if browser_type not in support_browsers:
logger.info("不支持的浏览器类型,浏览器类型为:{}", browser_type)
exit()

return eval(browserType.title() + "Browser")()
return eval(browser_type.title() + "Browser")()
14 changes: 7 additions & 7 deletions browser/chrome.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@


class ChromeBrowser(Browser):
def setting(self, config: Config = None, log_path: str = "", userDataDir: str = ""):
def setting(self, config: Config = None, log_path: str = "", user_data_dir: str = ""):
options = webdriver.ChromeOptions()

options.add_argument(r"--user-data-dir={}".format(userDataDir))
options.add_argument(r"--user-data-dir={}".format(user_data_dir))
options.add_argument(r"--profile-directory={}".format("Default"))
if config.getboolean("browser", "headless", False):
options.add_argument('--headless')
# headless 模式下需要设置user_agent及窗口大小,否则会被识别成移动端访问
defaultUserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36"
userAgent = config.get("browser", "userAgent", defaultUserAgent)
options.add_argument(r"user-agent={}".format(userAgent))
default_user_agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36"
user_agent = config.get("browser", "userAgent", default_user_agent)
options.add_argument(r"user-agent={}".format(user_agent))
options.add_argument("--window-size=1920,1080")

options.add_argument('--ignore-certificate-errors')
Expand All @@ -32,8 +32,8 @@ def setting(self, config: Config = None, log_path: str = "", userDataDir: str =
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--no-sandbox')

driverPath = config.get("browser", "driverPath", '')
executable_path = None if len(driverPath) < 1 else driverPath
driver_path = config.get("browser", "driverPath", '')
executable_path = None if len(driver_path) < 1 else driver_path
browser = webdriver.Chrome(service=webdriver.ChromeService(executable_path=executable_path, log_path=log_path),
options=options)
return browser
14 changes: 7 additions & 7 deletions browser/edge.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@


class EdgeBrowser(Browser):
def setting(self, config: Config = None, log_path: str = "", userDataDir: str = ""):
def setting(self, config: Config = None, log_path: str = "", user_data_dir: str = ""):
options = webdriver.EdgeOptions()

options.add_argument(r"--user-data-dir={}".format(userDataDir))
options.add_argument(r"--user-data-dir={}".format(user_data_dir))
options.add_argument(r"--profile-directory={}".format("Default"))
if config.getboolean("browser", "headless", False):
options.add_argument('--headless')
# headless 模式下需要设置user_agent及窗口大小,否则会被识别成移动端访问
defaultUserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36 Edg/117.0.2045.55"
userAgent = config.get("browser", "userAgent", defaultUserAgent)
options.add_argument(r"user-agent={}".format(userAgent))
default_user_agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36 Edg/117.0.2045.55"
user_agent = config.get("browser", "userAgent", default_user_agent)
options.add_argument(r"user-agent={}".format(user_agent))
options.add_argument("--window-size=1920,1080")

options.add_argument('--ignore-certificate-errors')
Expand All @@ -32,8 +32,8 @@ def setting(self, config: Config = None, log_path: str = "", userDataDir: str =
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--no-sandbox')

driverPath = config.get("browser", "driverPath", '')
executable_path = None if len(driverPath) < 1 else driverPath
driver_path = config.get("browser", "driverPath", '')
executable_path = None if len(driver_path) < 1 else driver_path
browser = webdriver.Edge(service=webdriver.EdgeService(executable_path=executable_path, log_path=log_path),
options=options)
return browser
12 changes: 6 additions & 6 deletions browser/firefox.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@

class FirefoxBrowser(Browser):

def setting(self, config: Config = None, log_path: str = "", userDataDir: str = ""):
def setting(self, config: Config = None, log_path: str = "", user_data_dir: str = ""):
options = webdriver.FirefoxOptions()

if config.getboolean("browser", "headless", False):
options.add_argument('--headless')
defaultUserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/118.0"
userAgent = config.get("browser", "userAgent", defaultUserAgent)
options.set_preference("general.useragent.override", userAgent)
default_user_agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/118.0"
user_agent = config.get("browser", "userAgent", default_user_agent)
options.set_preference("general.useragent.override", user_agent)

# options.add_argument(r"--profile-root={}".format(os.path.dirname(os.path.abspath(__file__))))
# options.add_argument(r"-profile={}".format("profiles"))

driverPath = config.get("browser", "driverPath", '')
executable_path = None if len(driverPath) < 1 else driverPath
driver_path = config.get("browser", "driverPath", '')
executable_path = None if len(driver_path) < 1 else driver_path
browser = webdriver.Firefox(
service=webdriver.FirefoxService(executable_path=executable_path, log_path=log_path),
options=options)
Expand Down
12 changes: 6 additions & 6 deletions browser/safari.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@

class SafariBrowser(Browser):

def setting(self, config: Config = None, log_path: str = "", userDataDir: str = ""):
def setting(self, config: Config = None, log_path: str = "", user_data_dir: str = ""):
options = webdriver.SafariOptions()

if config.getboolean("browser", "headless", False):
options.add_argument('--headless')
defaultUserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Safari/605.1.15"
userAgent = config.get("browser", "userAgent", defaultUserAgent)
options.add_argument(r"--user-agent={}".format(userAgent))
default_user_agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Safari/605.1.15"
user_agent = config.get("browser", "userAgent", default_user_agent)
options.add_argument(r"--user-agent={}".format(user_agent))
options.add_argument("--window-size=1920,1080")

options.add_argument('--ignore-certificate-errors')
Expand All @@ -27,8 +27,8 @@ def setting(self, config: Config = None, log_path: str = "", userDataDir: str =
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--no-sandbox')

driverPath = config.get("browser", "driverPath", '')
executable_path = None if len(driverPath) < 1 else driverPath
driver_path = config.get("browser", "driverPath", '')
executable_path = None if len(driver_path) < 1 else driver_path
browser = webdriver.Safari(service=webdriver.SafariService(executable_path=executable_path, log_path=log_path,
service_args=["--diagnose"]),
options=options)
Expand Down
3 changes: 3 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@
# !/usr/bin/python

from configparser import ConfigParser
from loguru import logger


class Config:
def __init__(self, filename, encoding="utf-8"):
logger.info("开始解析配置文件")
self.filename = filename
self.encoding = encoding
self.config = ConfigParser()
self.config.read(filename, encoding)
logger.info("结束解析配置文件")

def get(self, section, option, default_value=None):
if default_value is None:
Expand Down
Loading

0 comments on commit a52dc2b

Please sign in to comment.