Doiiars

PlatformCrawler: 修复多行赋值配置被替换后残留续行导致IndentationError

@@ -185,26 +185,42 @@ postgresql_db_config = {{ @@ -185,26 +185,42 @@ postgresql_db_config = {{
185 content = f.read() 185 content = f.read()
186 186
187 # 修改关键配置项 187 # 修改关键配置项
  188 + # skip_until_paren: 当原始行是多行赋值(以"("结尾)被替换为单行后,
  189 + # 需要跳过后续续行直到遇到配对的")"
188 lines = content.split('\n') 190 lines = content.split('\n')
189 new_lines = [] 191 new_lines = []
190 - 192 + skip_until_paren = False
  193 +
191 for line in lines: 194 for line in lines:
  195 + # 跳过多行赋值的续行
  196 + if skip_until_paren:
  197 + if line.strip() == ')':
  198 + skip_until_paren = False
  199 + continue
  200 +
  201 + replaced = None
192 if line.startswith('PLATFORM = '): 202 if line.startswith('PLATFORM = '):
193 - new_lines.append(f'PLATFORM = "{platform}" # 平台,xhs | dy | ks | bili | wb | tieba | zhihu') 203 + replaced = f'PLATFORM = "{platform}" # 平台,xhs | dy | ks | bili | wb | tieba | zhihu'
194 elif line.startswith('KEYWORDS = '): 204 elif line.startswith('KEYWORDS = '):
195 - new_lines.append(f'KEYWORDS = "{keywords_str}" # 关键词搜索配置,以英文逗号分隔') 205 + replaced = f'KEYWORDS = "{keywords_str}" # 关键词搜索配置,以英文逗号分隔'
196 elif line.startswith('CRAWLER_TYPE = '): 206 elif line.startswith('CRAWLER_TYPE = '):
197 - new_lines.append(f'CRAWLER_TYPE = "{crawler_type}" # 爬取类型,search(关键词搜索) | detail(帖子详情)| creator(创作者主页数据)') 207 + replaced = f'CRAWLER_TYPE = "{crawler_type}" # 爬取类型,search(关键词搜索) | detail(帖子详情)| creator(创作者主页数据)'
198 elif line.startswith('SAVE_DATA_OPTION = '): 208 elif line.startswith('SAVE_DATA_OPTION = '):
199 - new_lines.append(f'SAVE_DATA_OPTION = "{save_data_option}" # csv or db or json or sqlite or postgresql') 209 + replaced = f'SAVE_DATA_OPTION = "{save_data_option}" # csv or db or json or sqlite or postgresql'
200 elif line.startswith('CRAWLER_MAX_NOTES_COUNT = '): 210 elif line.startswith('CRAWLER_MAX_NOTES_COUNT = '):
201 - new_lines.append(f'CRAWLER_MAX_NOTES_COUNT = {max_notes}') 211 + replaced = f'CRAWLER_MAX_NOTES_COUNT = {max_notes}'
202 elif line.startswith('ENABLE_GET_COMMENTS = '): 212 elif line.startswith('ENABLE_GET_COMMENTS = '):
203 - new_lines.append('ENABLE_GET_COMMENTS = True') 213 + replaced = 'ENABLE_GET_COMMENTS = True'
204 elif line.startswith('CRAWLER_MAX_COMMENTS_COUNT_SINGLENOTES = '): 214 elif line.startswith('CRAWLER_MAX_COMMENTS_COUNT_SINGLENOTES = '):
205 - new_lines.append('CRAWLER_MAX_COMMENTS_COUNT_SINGLENOTES = 20') 215 + replaced = 'CRAWLER_MAX_COMMENTS_COUNT_SINGLENOTES = 20'
206 elif line.startswith('HEADLESS = '): 216 elif line.startswith('HEADLESS = '):
207 - new_lines.append('HEADLESS = True') # 使用无头模式 217 + replaced = 'HEADLESS = True'
  218 +
  219 + if replaced is not None:
  220 + new_lines.append(replaced)
  221 + # 若原始行是多行赋值开头(以"("结尾),跳过后续续行
  222 + if line.rstrip().endswith('('):
  223 + skip_until_paren = True
208 else: 224 else:
209 new_lines.append(line) 225 new_lines.append(line)
210 226