更新脚本附加同步信息
(self, script_url, script_id, attribute_default, additional_info)
| 200 | return 0 |
| 201 | |
| 202 | def sync_update(self, script_url, script_id, attribute_default, additional_info): |
| 203 | """ |
| 204 | 更新脚本附加同步信息 |
| 205 | """ |
| 206 | |
| 207 | # 历史代码.保留以备用 |
| 208 | if self.csrf_token is None: |
| 209 | self.fetch_csrf_token() |
| 210 | |
| 211 | # 按照顺序构建区域代码(与 GreasyFork 保持一致) |
| 212 | langmap = read_json('utils/docs/lang_map.json') |
| 213 | area = {} |
| 214 | index = 1 |
| 215 | for lang_dict in langmap["langs"]: |
| 216 | for lang_code in lang_dict.keys(): |
| 217 | area[lang_code] = str(index) |
| 218 | index += 1 |
| 219 | |
| 220 | # 设置表单数据 |
| 221 | form_data = { |
| 222 | '_method': 'patch', |
| 223 | 'authenticity_token': self.csrf_token, |
| 224 | 'script[sync_identifier]': script_url, |
| 225 | 'script[sync_type]': 'webhook', |
| 226 | 'update-and-sync': '更新设置并立即同步' |
| 227 | } |
| 228 | |
| 229 | # 默认的语言文件 |
| 230 | if attribute_default: |
| 231 | form_data['additional_info_sync[0][attribute_default]'] = 'true' |
| 232 | form_data['additional_info_sync[0][sync_identifier]'] = attribute_default |
| 233 | form_data['additional_info_sync[0][value_markup]'] = 'markdown' |
| 234 | |
| 235 | # 遍历每个 语言URL,用于构建区域化文件 |
| 236 | for index, url in enumerate(additional_info): |
| 237 | locale_key = extract_locale_key(url) |
| 238 | locale = area.get(locale_key, '') |
| 239 | clean_url = re.sub(r'##.*', '', url) |
| 240 | form_data[f'additional_info_sync[{index + 1}][attribute_default]'] = 'false' |
| 241 | form_data[f'additional_info_sync[{index + 1}][locale]'] = locale |
| 242 | form_data[f'additional_info_sync[{index + 1}][sync_identifier]'] = clean_url |
| 243 | form_data[f'additional_info_sync[{index + 1}][value_markup]'] = 'markdown' |
| 244 | response = self.session.post(f"https://greasyfork.org/zh-CN/scripts/{script_id}/sync_update", data=form_data, headers={'Content-Type': 'application/x-www-form-urlencoded'}) |
| 245 | soup = BeautifulSoup(response.text, 'html.parser') |
| 246 | script_name = soup.select_one('#script-info > header > h2') |
| 247 | specific_element = soup.select_one('body > div.width-constraint > p') |
| 248 | return f"[{script_name.get_text()}]:{specific_element.get_text()}" if specific_element else None |
| 249 | |
| 250 | |
| 251 | if __name__ == "__main__": |
no test coverage detected