ayugespidertools 3.9.2__tar.gz → 3.9.3__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (91) hide show
  1. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/PKG-INFO +1 -1
  2. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/__init__.py +1 -1
  3. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/common/expend.py +2 -0
  4. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/common/multiplexing.py +0 -1
  5. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/common/spiderconf.py +320 -313
  6. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/extras/cvnpil.py +1 -0
  7. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/scraper/pipelines/postgres/__init__.py +0 -1
  8. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/scraper/pipelines/postgres/asynced.py +10 -3
  9. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/pyproject.toml +1 -1
  10. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/LICENSE +0 -0
  11. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/README.md +0 -0
  12. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/commands/__init__.py +0 -0
  13. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/commands/crawl.py +0 -0
  14. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/commands/genspider.py +0 -0
  15. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/commands/startproject.py +0 -0
  16. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/commands/version.py +0 -0
  17. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/common/__init__.py +0 -0
  18. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/common/encryption.py +0 -0
  19. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/common/mongodbpipe.py +0 -0
  20. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/common/mysqlerrhandle.py +0 -0
  21. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/common/params.py +0 -0
  22. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/common/postgreserrhandle.py +0 -0
  23. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/common/sqlformat.py +0 -0
  24. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/common/typevars.py +0 -0
  25. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/common/utils.py +0 -0
  26. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/config.py +0 -0
  27. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/extras/__init__.py +0 -0
  28. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/extras/ext.py +0 -0
  29. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/formatdata.py +0 -0
  30. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/items.py +0 -0
  31. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/middlewares.py +0 -0
  32. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/mongoclient.py +0 -0
  33. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/mysqlclient.py +0 -0
  34. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/pipelines.py +0 -0
  35. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/request.py +0 -0
  36. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/scraper/__init__.py +0 -0
  37. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/scraper/http/__init__.py +0 -0
  38. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/scraper/http/request/__init__.py +0 -0
  39. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/scraper/http/request/form.py +0 -0
  40. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/scraper/middlewares/__init__.py +0 -0
  41. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/scraper/middlewares/headers/__init__.py +0 -0
  42. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/scraper/middlewares/headers/ua.py +0 -0
  43. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/scraper/middlewares/netlib/__init__.py +0 -0
  44. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/scraper/middlewares/netlib/aiohttplib.py +0 -0
  45. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/scraper/middlewares/proxy/__init__.py +0 -0
  46. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/scraper/middlewares/proxy/dynamic.py +0 -0
  47. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/scraper/middlewares/proxy/exclusive.py +0 -0
  48. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/scraper/pipelines/__init__.py +0 -0
  49. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/scraper/pipelines/download/__init__.py +0 -0
  50. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/scraper/pipelines/download/file.py +0 -0
  51. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/scraper/pipelines/mongo/__init__.py +0 -0
  52. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/scraper/pipelines/mongo/asynced.py +0 -0
  53. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/scraper/pipelines/mongo/fantasy.py +0 -0
  54. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/scraper/pipelines/mongo/twisted.py +0 -0
  55. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/scraper/pipelines/msgproducer/__init__.py +0 -0
  56. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/scraper/pipelines/msgproducer/kafkapub.py +0 -0
  57. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/scraper/pipelines/msgproducer/mqpub.py +0 -0
  58. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/scraper/pipelines/mysql/__init__.py +0 -0
  59. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/scraper/pipelines/mysql/asynced.py +0 -0
  60. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/scraper/pipelines/mysql/fantasy.py +0 -0
  61. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/scraper/pipelines/mysql/stats.py +0 -0
  62. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/scraper/pipelines/mysql/turbo.py +0 -0
  63. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/scraper/pipelines/mysql/twisted.py +0 -0
  64. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/scraper/pipelines/oracle/__init__.py +0 -0
  65. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/scraper/pipelines/oracle/fantasy.py +0 -0
  66. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/scraper/pipelines/oracle/twisted.py +0 -0
  67. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/scraper/pipelines/postgres/fantasy.py +0 -0
  68. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/scraper/pipelines/postgres/twisted.py +0 -0
  69. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/scraper/spiders/__init__.py +0 -0
  70. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/scraper/spiders/crawl.py +0 -0
  71. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/spiders.py +0 -0
  72. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/templates/project/README.md +0 -0
  73. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/templates/project/module/VIT/.conf +0 -0
  74. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/templates/project/module/__init__.py +0 -0
  75. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/templates/project/module/items.py.tmpl +0 -0
  76. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/templates/project/module/middlewares.py.tmpl +0 -0
  77. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/templates/project/module/pipelines.py.tmpl +0 -0
  78. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/templates/project/module/run.py.tmpl +0 -0
  79. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/templates/project/module/run.sh.tmpl +0 -0
  80. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/templates/project/module/settings.py.tmpl +0 -0
  81. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/templates/project/module/spiders/__init__.py +0 -0
  82. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/templates/project/pyproject.toml +0 -0
  83. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/templates/project/requirements.txt +0 -0
  84. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/templates/project/scrapy.cfg +0 -0
  85. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/templates/spiders/async.tmpl +0 -0
  86. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/templates/spiders/basic.tmpl +0 -0
  87. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/templates/spiders/crawl.tmpl +0 -0
  88. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/templates/spiders/csvfeed.tmpl +0 -0
  89. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/templates/spiders/xmlfeed.tmpl +0 -0
  90. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/utils/__init__.py +0 -0
  91. {ayugespidertools-3.9.2 → ayugespidertools-3.9.3}/ayugespidertools/utils/cmdline.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: AyugeSpiderTools
3
- Version: 3.9.2
3
+ Version: 3.9.3
4
4
  Summary: scrapy 扩展库:用于扩展 Scrapy 功能来解放双手。
5
5
  Home-page: https://www.ayuge.top/mkdocs-material/
6
6
  Keywords: crawler,scraping,aiohttp,asyncio,scrapy
@@ -13,4 +13,4 @@ __all__ = [
13
13
  "__version__",
14
14
  ]
15
15
 
16
- __version__ = "3.9.2"
16
+ __version__ = "3.9.3"
@@ -29,6 +29,8 @@ if TYPE_CHECKING:
29
29
  from ayugespidertools.common.typevars import MysqlConf, OracleConf, PostgreSQLConf
30
30
 
31
31
 
32
+
33
+
32
34
  class MysqlPipeEnhanceMixin:
33
35
  """扩展 mysql pipelines 的功能"""
34
36
 
@@ -23,7 +23,6 @@ except ImportError:
23
23
  # pip install ayugespidertools[database]
24
24
  pass
25
25
 
26
-
27
26
  __all__ = [
28
27
  "ReuseOperation",
29
28
  ]
@@ -1,313 +1,320 @@
1
- from abc import ABC, abstractmethod
2
- from typing import TYPE_CHECKING, Generic, Optional, Tuple, TypeVar
3
-
4
- from oracledb.exceptions import DatabaseError
5
- from sqlalchemy.exc import OperationalError
6
-
7
- from ayugespidertools.common.typevars import (
8
- DatabaseEngineClass,
9
- DynamicProxyConf,
10
- ExclusiveProxyConf,
11
- KafkaConf,
12
- MongoDBConf,
13
- MQConf,
14
- MysqlConf,
15
- OracleConf,
16
- PostgreSQLConf,
17
- )
18
- from ayugespidertools.common.utils import ToolsForAyu
19
- from ayugespidertools.config import logger
20
-
21
- __all__ = [
22
- "get_spider_conf",
23
- "MysqlConfCreator",
24
- "MongoDBConfCreator",
25
- "MQConfCreator",
26
- "OracleConfCreator",
27
- "PostgreSQLConfCreator",
28
- "KafkaConfCreator",
29
- "DynamicProxyCreator",
30
- "ExclusiveProxyCreator",
31
- "get_sqlalchemy_conf",
32
- ]
33
-
34
- if TYPE_CHECKING:
35
- from scrapy.settings import Settings
36
- from sqlalchemy.engine.base import Connection as SqlalchemyConnectT
37
- from sqlalchemy.engine.base import Engine as SqlalchemyEngineT
38
-
39
- SpiderConf = TypeVar(
40
- "SpiderConf",
41
- MysqlConf,
42
- MongoDBConf,
43
- MQConf,
44
- KafkaConf,
45
- DynamicProxyConf,
46
- ExclusiveProxyConf,
47
- )
48
-
49
-
50
- class Product(ABC, Generic[SpiderConf]):
51
- @abstractmethod
52
- def get_conn_conf(
53
- self, settings: "Settings", remote_option: dict
54
- ) -> Optional[SpiderConf]:
55
- """获取各个工具链接配置信息"""
56
- pass
57
-
58
- @abstractmethod
59
- def get_engine(
60
- self, db_conf: SpiderConf, db_engine_enabled: bool
61
- ) -> Tuple[Optional["SqlalchemyEngineT"], Optional["SqlalchemyConnectT"]]:
62
- """获取各个工具中对应的 sqlalchemy db_engine 和 db_engine_conn。
63
- 需要此方法的工具有 mysql,postgresql,mongodb,oracle,其余的不需要。
64
- """
65
- pass
66
-
67
-
68
- class Creator(ABC):
69
- @abstractmethod
70
- def create_product(self) -> Product:
71
- pass
72
-
73
-
74
- class MysqlConfProduct(Product):
75
- def get_conn_conf(
76
- self, settings: "Settings", remote_option: dict
77
- ) -> Optional[MysqlConf]:
78
- # 1). 优先从远程配置中取值
79
- if settings.get("APP_CONF_MANAGE", False):
80
- remote_conf = ToolsForAyu.fetch_remote_conf(
81
- conf_name="mysql", **remote_option
82
- )
83
- return MysqlConf(**remote_conf) if remote_conf else None
84
-
85
- # 2). 从本地参数中取值
86
- local_conf = settings.get("MYSQL_CONFIG")
87
- return MysqlConf(**local_conf) if local_conf else None
88
-
89
- def get_engine(
90
- self, db_conf: MysqlConf, db_engine_enabled: bool
91
- ) -> Tuple[Optional["SqlalchemyEngineT"], Optional["SqlalchemyConnectT"]]:
92
- mysql_engine = mysql_engine_conn = None
93
- if db_engine_enabled:
94
- mysql_url = (
95
- f"mysql+pymysql://{db_conf.user}"
96
- f":{db_conf.password}@{db_conf.host}"
97
- f":{db_conf.port}/{db_conf.database}"
98
- f"?charset={db_conf.charset}"
99
- )
100
- try:
101
- mysql_engine = DatabaseEngineClass(engine_url=mysql_url).engine
102
- mysql_engine_conn = mysql_engine.connect()
103
- except OperationalError as err:
104
- logger.warning(f"MySQL engine enabled failed: {err}")
105
- return mysql_engine, mysql_engine_conn
106
-
107
-
108
- class MongoDBConfProduct(Product):
109
- def get_conn_conf(
110
- self, settings: "Settings", remote_option: dict
111
- ) -> Optional[MongoDBConf]:
112
- if settings.get("APP_CONF_MANAGE", False):
113
- remote_conf = ToolsForAyu.fetch_remote_conf(
114
- conf_name="mongodb", **remote_option
115
- )
116
- return MongoDBConf(**remote_conf) if remote_conf else None
117
-
118
- local_conf = settings.get("MONGODB_CONFIG")
119
- return MongoDBConf(**local_conf) if local_conf else None
120
-
121
- def get_engine(self, db_conf: MongoDBConf, db_engine_enabled: bool):
122
- pass
123
-
124
-
125
- class PostgreSQLConfProduct(Product):
126
- def get_conn_conf(
127
- self, settings: "Settings", remote_option: dict
128
- ) -> Optional[PostgreSQLConf]:
129
- if settings.get("APP_CONF_MANAGE", False):
130
- remote_conf = ToolsForAyu.fetch_remote_conf(
131
- conf_name="postgresql", **remote_option
132
- )
133
- return PostgreSQLConf(**remote_conf) if remote_conf else None
134
-
135
- local_conf = settings.get("POSTGRESQL_CONFIG")
136
- return PostgreSQLConf(**local_conf) if local_conf else None
137
-
138
- def get_engine(
139
- self, db_conf: PostgreSQLConf, db_engine_enabled: bool
140
- ) -> Tuple[Optional["SqlalchemyEngineT"], Optional["SqlalchemyConnectT"]]:
141
- postgres_engine = postgres_engine_conn = None
142
- if db_engine_enabled:
143
- postgres_url = (
144
- f"postgresql+psycopg://{db_conf.user}:{db_conf.password}"
145
- f"@{db_conf.host}:{db_conf.port}/{db_conf.database}"
146
- )
147
- try:
148
- postgres_engine = DatabaseEngineClass(engine_url=postgres_url).engine
149
- postgres_engine_conn = postgres_engine.connect()
150
- except OperationalError as err:
151
- logger.warning(f"PostgreSQL engine enabled failed: {err}")
152
- return postgres_engine, postgres_engine_conn
153
-
154
-
155
- class OracleConfProduct(Product):
156
- def get_conn_conf(
157
- self, settings: "Settings", remote_option: dict
158
- ) -> Optional[OracleConf]:
159
- if settings.get("APP_CONF_MANAGE", False):
160
- remote_conf = ToolsForAyu.fetch_remote_conf(
161
- conf_name="oracle", **remote_option
162
- )
163
- return OracleConf(**remote_conf) if remote_conf else None
164
-
165
- local_conf = settings.get("ORACLE_CONFIG")
166
- return OracleConf(**local_conf) if local_conf else None
167
-
168
- def get_engine(
169
- self, db_conf: OracleConf, db_engine_enabled: bool
170
- ) -> Tuple[Optional["SqlalchemyEngineT"], Optional["SqlalchemyConnectT"]]:
171
- oracle_engine = oracle_engine_conn = None
172
- if db_engine_enabled:
173
- oracle_url = (
174
- f"oracle+oracledb://{db_conf.user}:{db_conf.password}"
175
- f"@{db_conf.host}:{db_conf.port}/{db_conf.service_name}"
176
- )
177
- thick_mode = (
178
- {"lib_dir": db_conf.thick_lib_dir} if db_conf.thick_lib_dir else False
179
- )
180
-
181
- try:
182
- oracle_engine = DatabaseEngineClass(
183
- engine_url=oracle_url,
184
- thick_mode=thick_mode,
185
- ).engine
186
- oracle_engine_conn = oracle_engine.connect()
187
- except (OperationalError, DatabaseError) as err:
188
- logger.warning(f"Oracle engine enabled failed: {err}")
189
- return oracle_engine, oracle_engine_conn
190
-
191
-
192
- class MQConfProduct(Product):
193
- def get_conn_conf(
194
- self, settings: "Settings", remote_option: dict
195
- ) -> Optional[MQConf]:
196
- if settings.get("APP_CONF_MANAGE", False):
197
- remote_conf = ToolsForAyu.fetch_remote_conf(
198
- conf_name="rabbitmq", **remote_option
199
- )
200
- return MQConf(**remote_conf) if remote_conf else None
201
-
202
- local_conf = settings.get("MQ_CONFIG")
203
- return MQConf(**local_conf) if local_conf else None
204
-
205
- def get_engine(self, db_conf: MQConf, db_engine_enabled: bool):
206
- pass
207
-
208
-
209
- class KafkaConfProduct(Product):
210
- def get_conn_conf(
211
- self, settings: "Settings", remote_option: dict
212
- ) -> Optional[KafkaConf]:
213
- if settings.get("APP_CONF_MANAGE", False):
214
- remote_conf = ToolsForAyu.fetch_remote_conf(
215
- conf_name="kafka", **remote_option
216
- )
217
- return KafkaConf(**remote_conf) if remote_conf else None
218
-
219
- local_conf = settings.get("KAFKA_CONFIG")
220
- return KafkaConf(**local_conf) if local_conf else None
221
-
222
- def get_engine(self, db_conf: KafkaConf, db_engine_enabled: bool):
223
- pass
224
-
225
-
226
- class DynamicProxyProduct(Product):
227
- def get_conn_conf(
228
- self, settings: "Settings", remote_option: dict
229
- ) -> Optional[DynamicProxyConf]:
230
- if settings.get("APP_CONF_MANAGE", False):
231
- remote_conf = ToolsForAyu.fetch_remote_conf(
232
- conf_name="dynamicproxy", **remote_option
233
- )
234
- return DynamicProxyConf(**remote_conf) if remote_conf else None
235
-
236
- local_conf = settings.get("DYNAMIC_PROXY_CONFIG")
237
- return DynamicProxyConf(**local_conf) if local_conf else None
238
-
239
- def get_engine(self, db_conf: DynamicProxyConf, db_engine_enabled: bool):
240
- pass
241
-
242
-
243
- class ExclusiveProxyProduct(Product):
244
- def get_conn_conf(
245
- self, settings: "Settings", remote_option: dict
246
- ) -> Optional[ExclusiveProxyConf]:
247
- if settings.get("APP_CONF_MANAGE", False):
248
- remote_conf = ToolsForAyu.fetch_remote_conf(
249
- conf_name="exclusiveproxy", **remote_option
250
- )
251
- return ExclusiveProxyConf(**remote_conf) if remote_conf else None
252
-
253
- local_conf = settings.get("EXCLUSIVE_PROXY_CONFIG")
254
- return ExclusiveProxyConf(**local_conf) if local_conf else None
255
-
256
- def get_engine(self, db_conf: ExclusiveProxyConf, db_engine_enabled: bool):
257
- pass
258
-
259
-
260
- class MysqlConfCreator(Creator):
261
- def create_product(self) -> Product:
262
- return MysqlConfProduct()
263
-
264
-
265
- class MongoDBConfCreator(Creator):
266
- def create_product(self) -> Product:
267
- return MongoDBConfProduct()
268
-
269
-
270
- class PostgreSQLConfCreator(Creator):
271
- def create_product(self) -> Product:
272
- return PostgreSQLConfProduct()
273
-
274
-
275
- class OracleConfCreator(Creator):
276
- def create_product(self) -> Product:
277
- return OracleConfProduct()
278
-
279
-
280
- class MQConfCreator(Creator):
281
- def create_product(self) -> Product:
282
- return MQConfProduct()
283
-
284
-
285
- class KafkaConfCreator(Creator):
286
- def create_product(self) -> Product:
287
- return KafkaConfProduct()
288
-
289
-
290
- class DynamicProxyCreator(Creator):
291
- def create_product(self) -> Product:
292
- return DynamicProxyProduct()
293
-
294
-
295
- class ExclusiveProxyCreator(Creator):
296
- def create_product(self) -> Product:
297
- return ExclusiveProxyProduct()
298
-
299
-
300
- def get_spider_conf(
301
- creator: Creator, settings: "Settings", remote_option: dict
302
- ) -> Optional[SpiderConf]:
303
- product = creator.create_product()
304
- return product.get_conn_conf(settings, remote_option)
305
-
306
-
307
- def get_sqlalchemy_conf(
308
- creator: Creator,
309
- db_conf: SpiderConf,
310
- db_engine_enabled: bool,
311
- ) -> Tuple[Optional["SqlalchemyEngineT"], Optional["SqlalchemyConnectT"]]:
312
- product = creator.create_product()
313
- return product.get_engine(db_conf, db_engine_enabled)
1
+ from abc import ABC, abstractmethod
2
+ from typing import TYPE_CHECKING, Generic, Optional, Tuple, TypeVar
3
+
4
+ from sqlalchemy.exc import OperationalError
5
+
6
+ from ayugespidertools.common.typevars import (
7
+ DatabaseEngineClass,
8
+ DynamicProxyConf,
9
+ ExclusiveProxyConf,
10
+ KafkaConf,
11
+ MongoDBConf,
12
+ MQConf,
13
+ MysqlConf,
14
+ OracleConf,
15
+ PostgreSQLConf,
16
+ )
17
+ from ayugespidertools.common.utils import ToolsForAyu
18
+ from ayugespidertools.config import logger
19
+
20
+ try:
21
+ from oracledb.exceptions import DatabaseError
22
+ except ImportError:
23
+ # pip install ayugespidertools[database]
24
+ pass
25
+
26
+ __all__ = [
27
+ "get_spider_conf",
28
+ "MysqlConfCreator",
29
+ "MongoDBConfCreator",
30
+ "MQConfCreator",
31
+ "OracleConfCreator",
32
+ "PostgreSQLConfCreator",
33
+ "KafkaConfCreator",
34
+ "DynamicProxyCreator",
35
+ "ExclusiveProxyCreator",
36
+ "get_sqlalchemy_conf",
37
+ ]
38
+
39
+ if TYPE_CHECKING:
40
+ from scrapy.settings import Settings
41
+ from sqlalchemy.engine.base import Connection as SqlalchemyConnectT
42
+ from sqlalchemy.engine.base import Engine as SqlalchemyEngineT
43
+
44
+ SpiderConf = TypeVar(
45
+ "SpiderConf",
46
+ MysqlConf,
47
+ MongoDBConf,
48
+ MQConf,
49
+ KafkaConf,
50
+ DynamicProxyConf,
51
+ ExclusiveProxyConf,
52
+ )
53
+
54
+
55
+
56
+
57
+ class Product(ABC, Generic[SpiderConf]):
58
+ @abstractmethod
59
+ def get_conn_conf(
60
+ self, settings: "Settings", remote_option: dict
61
+ ) -> Optional[SpiderConf]:
62
+ """获取各个工具链接配置信息"""
63
+ pass
64
+
65
+ @abstractmethod
66
+ def get_engine(
67
+ self, db_conf: SpiderConf, db_engine_enabled: bool
68
+ ) -> Tuple[Optional["SqlalchemyEngineT"], Optional["SqlalchemyConnectT"]]:
69
+ """获取各个工具中对应的 sqlalchemy db_engine 和 db_engine_conn。
70
+ 需要此方法的工具有 mysql,postgresql,mongodb,oracle,其余的不需要。
71
+ """
72
+ pass
73
+
74
+
75
+ class Creator(ABC):
76
+ @abstractmethod
77
+ def create_product(self) -> Product:
78
+ pass
79
+
80
+
81
+ class MysqlConfProduct(Product):
82
+ def get_conn_conf(
83
+ self, settings: "Settings", remote_option: dict
84
+ ) -> Optional[MysqlConf]:
85
+ # 1). 优先从远程配置中取值
86
+ if settings.get("APP_CONF_MANAGE", False):
87
+ remote_conf = ToolsForAyu.fetch_remote_conf(
88
+ conf_name="mysql", **remote_option
89
+ )
90
+ return MysqlConf(**remote_conf) if remote_conf else None
91
+
92
+ # 2). 从本地参数中取值
93
+ local_conf = settings.get("MYSQL_CONFIG")
94
+ return MysqlConf(**local_conf) if local_conf else None
95
+
96
+ def get_engine(
97
+ self, db_conf: MysqlConf, db_engine_enabled: bool
98
+ ) -> Tuple[Optional["SqlalchemyEngineT"], Optional["SqlalchemyConnectT"]]:
99
+ mysql_engine = mysql_engine_conn = None
100
+ if db_engine_enabled:
101
+ mysql_url = (
102
+ f"mysql+pymysql://{db_conf.user}"
103
+ f":{db_conf.password}@{db_conf.host}"
104
+ f":{db_conf.port}/{db_conf.database}"
105
+ f"?charset={db_conf.charset}"
106
+ )
107
+ try:
108
+ mysql_engine = DatabaseEngineClass(engine_url=mysql_url).engine
109
+ mysql_engine_conn = mysql_engine.connect()
110
+ except OperationalError as err:
111
+ logger.warning(f"MySQL engine enabled failed: {err}")
112
+ return mysql_engine, mysql_engine_conn
113
+
114
+
115
+ class MongoDBConfProduct(Product):
116
+ def get_conn_conf(
117
+ self, settings: "Settings", remote_option: dict
118
+ ) -> Optional[MongoDBConf]:
119
+ if settings.get("APP_CONF_MANAGE", False):
120
+ remote_conf = ToolsForAyu.fetch_remote_conf(
121
+ conf_name="mongodb", **remote_option
122
+ )
123
+ return MongoDBConf(**remote_conf) if remote_conf else None
124
+
125
+ local_conf = settings.get("MONGODB_CONFIG")
126
+ return MongoDBConf(**local_conf) if local_conf else None
127
+
128
+ def get_engine(self, db_conf: MongoDBConf, db_engine_enabled: bool):
129
+ pass
130
+
131
+
132
+ class PostgreSQLConfProduct(Product):
133
+ def get_conn_conf(
134
+ self, settings: "Settings", remote_option: dict
135
+ ) -> Optional[PostgreSQLConf]:
136
+ if settings.get("APP_CONF_MANAGE", False):
137
+ remote_conf = ToolsForAyu.fetch_remote_conf(
138
+ conf_name="postgresql", **remote_option
139
+ )
140
+ return PostgreSQLConf(**remote_conf) if remote_conf else None
141
+
142
+ local_conf = settings.get("POSTGRESQL_CONFIG")
143
+ return PostgreSQLConf(**local_conf) if local_conf else None
144
+
145
+ def get_engine(
146
+ self, db_conf: PostgreSQLConf, db_engine_enabled: bool
147
+ ) -> Tuple[Optional["SqlalchemyEngineT"], Optional["SqlalchemyConnectT"]]:
148
+ postgres_engine = postgres_engine_conn = None
149
+ if db_engine_enabled:
150
+ postgres_url = (
151
+ f"postgresql+psycopg://{db_conf.user}:{db_conf.password}"
152
+ f"@{db_conf.host}:{db_conf.port}/{db_conf.database}"
153
+ )
154
+ try:
155
+ postgres_engine = DatabaseEngineClass(engine_url=postgres_url).engine
156
+ postgres_engine_conn = postgres_engine.connect()
157
+ except OperationalError as err:
158
+ logger.warning(f"PostgreSQL engine enabled failed: {err}")
159
+ return postgres_engine, postgres_engine_conn
160
+
161
+
162
+ class OracleConfProduct(Product):
163
+ def get_conn_conf(
164
+ self, settings: "Settings", remote_option: dict
165
+ ) -> Optional[OracleConf]:
166
+ if settings.get("APP_CONF_MANAGE", False):
167
+ remote_conf = ToolsForAyu.fetch_remote_conf(
168
+ conf_name="oracle", **remote_option
169
+ )
170
+ return OracleConf(**remote_conf) if remote_conf else None
171
+
172
+ local_conf = settings.get("ORACLE_CONFIG")
173
+ return OracleConf(**local_conf) if local_conf else None
174
+
175
+ def get_engine(
176
+ self, db_conf: OracleConf, db_engine_enabled: bool
177
+ ) -> Tuple[Optional["SqlalchemyEngineT"], Optional["SqlalchemyConnectT"]]:
178
+ oracle_engine = oracle_engine_conn = None
179
+ if db_engine_enabled:
180
+ oracle_url = (
181
+ f"oracle+oracledb://{db_conf.user}:{db_conf.password}"
182
+ f"@{db_conf.host}:{db_conf.port}/{db_conf.service_name}"
183
+ )
184
+ thick_mode = (
185
+ {"lib_dir": db_conf.thick_lib_dir} if db_conf.thick_lib_dir else False
186
+ )
187
+
188
+ try:
189
+ oracle_engine = DatabaseEngineClass(
190
+ engine_url=oracle_url,
191
+ thick_mode=thick_mode,
192
+ ).engine
193
+ oracle_engine_conn = oracle_engine.connect()
194
+ except (OperationalError, DatabaseError) as err:
195
+ logger.warning(f"Oracle engine enabled failed: {err}")
196
+ return oracle_engine, oracle_engine_conn
197
+
198
+
199
+ class MQConfProduct(Product):
200
+ def get_conn_conf(
201
+ self, settings: "Settings", remote_option: dict
202
+ ) -> Optional[MQConf]:
203
+ if settings.get("APP_CONF_MANAGE", False):
204
+ remote_conf = ToolsForAyu.fetch_remote_conf(
205
+ conf_name="rabbitmq", **remote_option
206
+ )
207
+ return MQConf(**remote_conf) if remote_conf else None
208
+
209
+ local_conf = settings.get("MQ_CONFIG")
210
+ return MQConf(**local_conf) if local_conf else None
211
+
212
+ def get_engine(self, db_conf: MQConf, db_engine_enabled: bool):
213
+ pass
214
+
215
+
216
+ class KafkaConfProduct(Product):
217
+ def get_conn_conf(
218
+ self, settings: "Settings", remote_option: dict
219
+ ) -> Optional[KafkaConf]:
220
+ if settings.get("APP_CONF_MANAGE", False):
221
+ remote_conf = ToolsForAyu.fetch_remote_conf(
222
+ conf_name="kafka", **remote_option
223
+ )
224
+ return KafkaConf(**remote_conf) if remote_conf else None
225
+
226
+ local_conf = settings.get("KAFKA_CONFIG")
227
+ return KafkaConf(**local_conf) if local_conf else None
228
+
229
+ def get_engine(self, db_conf: KafkaConf, db_engine_enabled: bool):
230
+ pass
231
+
232
+
233
+ class DynamicProxyProduct(Product):
234
+ def get_conn_conf(
235
+ self, settings: "Settings", remote_option: dict
236
+ ) -> Optional[DynamicProxyConf]:
237
+ if settings.get("APP_CONF_MANAGE", False):
238
+ remote_conf = ToolsForAyu.fetch_remote_conf(
239
+ conf_name="dynamicproxy", **remote_option
240
+ )
241
+ return DynamicProxyConf(**remote_conf) if remote_conf else None
242
+
243
+ local_conf = settings.get("DYNAMIC_PROXY_CONFIG")
244
+ return DynamicProxyConf(**local_conf) if local_conf else None
245
+
246
+ def get_engine(self, db_conf: DynamicProxyConf, db_engine_enabled: bool):
247
+ pass
248
+
249
+
250
+ class ExclusiveProxyProduct(Product):
251
+ def get_conn_conf(
252
+ self, settings: "Settings", remote_option: dict
253
+ ) -> Optional[ExclusiveProxyConf]:
254
+ if settings.get("APP_CONF_MANAGE", False):
255
+ remote_conf = ToolsForAyu.fetch_remote_conf(
256
+ conf_name="exclusiveproxy", **remote_option
257
+ )
258
+ return ExclusiveProxyConf(**remote_conf) if remote_conf else None
259
+
260
+ local_conf = settings.get("EXCLUSIVE_PROXY_CONFIG")
261
+ return ExclusiveProxyConf(**local_conf) if local_conf else None
262
+
263
+ def get_engine(self, db_conf: ExclusiveProxyConf, db_engine_enabled: bool):
264
+ pass
265
+
266
+
267
+ class MysqlConfCreator(Creator):
268
+ def create_product(self) -> Product:
269
+ return MysqlConfProduct()
270
+
271
+
272
+ class MongoDBConfCreator(Creator):
273
+ def create_product(self) -> Product:
274
+ return MongoDBConfProduct()
275
+
276
+
277
+ class PostgreSQLConfCreator(Creator):
278
+ def create_product(self) -> Product:
279
+ return PostgreSQLConfProduct()
280
+
281
+
282
+ class OracleConfCreator(Creator):
283
+ def create_product(self) -> Product:
284
+ return OracleConfProduct()
285
+
286
+
287
+ class MQConfCreator(Creator):
288
+ def create_product(self) -> Product:
289
+ return MQConfProduct()
290
+
291
+
292
+ class KafkaConfCreator(Creator):
293
+ def create_product(self) -> Product:
294
+ return KafkaConfProduct()
295
+
296
+
297
+ class DynamicProxyCreator(Creator):
298
+ def create_product(self) -> Product:
299
+ return DynamicProxyProduct()
300
+
301
+
302
+ class ExclusiveProxyCreator(Creator):
303
+ def create_product(self) -> Product:
304
+ return ExclusiveProxyProduct()
305
+
306
+
307
+ def get_spider_conf(
308
+ creator: Creator, settings: "Settings", remote_option: dict
309
+ ) -> Optional[SpiderConf]:
310
+ product = creator.create_product()
311
+ return product.get_conn_conf(settings, remote_option)
312
+
313
+
314
+ def get_sqlalchemy_conf(
315
+ creator: Creator,
316
+ db_conf: SpiderConf,
317
+ db_engine_enabled: bool,
318
+ ) -> Tuple[Optional["SqlalchemyEngineT"], Optional["SqlalchemyConnectT"]]:
319
+ product = creator.create_product()
320
+ return product.get_engine(db_conf, db_engine_enabled)
@@ -16,6 +16,7 @@ if TYPE_CHECKING:
16
16
  from cv2.typing import MatLike
17
17
 
18
18
 
19
+
19
20
  class CvnpilKit:
20
21
  @staticmethod
21
22
  def get_array_dimension(array: Union[frozenset, list, set, tuple]) -> int:
@@ -6,7 +6,6 @@ from ayugespidertools.common.postgreserrhandle import Synchronize, deal_postgres
6
6
 
7
7
  __all__ = ["AyuPostgresPipeline"]
8
8
 
9
-
10
9
  if TYPE_CHECKING:
11
10
  from psycopg.connection import Connection
12
11
  from psycopg.cursor import Cursor
@@ -1,18 +1,25 @@
1
1
  from typing import TYPE_CHECKING, Optional
2
2
 
3
- from psycopg_pool import AsyncConnectionPool
4
3
  from scrapy.utils.defer import deferred_from_coro
5
4
 
6
5
  from ayugespidertools.common.expend import PostgreSQLPipeEnhanceMixin
7
6
  from ayugespidertools.common.multiplexing import ReuseOperation
8
7
 
9
- if TYPE_CHECKING:
10
- from ayugespidertools.common.typevars import PostgreSQLConf
8
+ try:
9
+ from psycopg_pool import AsyncConnectionPool
10
+ except ImportError:
11
+ # pip install ayugespidertools[database]
12
+ pass
11
13
 
12
14
  __all__ = [
13
15
  "AyuAsyncPostgresPipeline",
14
16
  ]
15
17
 
18
+ if TYPE_CHECKING:
19
+ from ayugespidertools.common.typevars import PostgreSQLConf
20
+
21
+
22
+
16
23
 
17
24
  class AyuAsyncPostgresPipeline(PostgreSQLPipeEnhanceMixin):
18
25
  """postgresql asyncio 版本"""
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "AyugeSpiderTools"
3
- version = "3.9.2"
3
+ version = "3.9.3"
4
4
  description = "scrapy 扩展库:用于扩展 Scrapy 功能来解放双手。"
5
5
  authors = ["ayuge <ayugesheng@gmail.com>"]
6
6
  maintainers = ["ayuge <ayugesheng@gmail.com>"]