导入脚本并提取脚本ID # NOTE (可以多个脚本同时导入,不知道返回的列表是否与导入顺序一致,没测试过.)
(self, sync_urls)
| 153 | return self.csrf_token |
| 154 | |
| 155 | def import_scripts(self, sync_urls): |
| 156 | """ |
| 157 | 导入脚本并提取脚本ID |
| 158 | # NOTE (可以多个脚本同时导入,不知道返回的列表是否与导入顺序一致,没测试过.) |
| 159 | """ |
| 160 | |
| 161 | # 刷新 CSRF Token,历史代码.无需调用. |
| 162 | self.fetch_csrf_token() |
| 163 | import_url = 'https://greasyfork.org/zh-CN/import/add' |
| 164 | headers = { |
| 165 | 'Content-Type': 'application/x-www-form-urlencoded' |
| 166 | } |
| 167 | |
| 168 | data = { |
| 169 | 'authenticity_token': self.csrf_token, |
| 170 | 'sync_urls': sync_urls, |
| 171 | 'sync-type': 'automatic', |
| 172 | 'commit': '导入' |
| 173 | } |
| 174 | response = self.session.post(import_url, headers=headers, data=data) |
| 175 | |
| 176 | # 解析返回的脚本ID信息 |
| 177 | if response.status_code == 200: |
| 178 | soup = BeautifulSoup(response.text, 'html.parser') |
| 179 | |
| 180 | # 提取返回的脚本列表 |
| 181 | results = [] # TODO 用于存储解析后的数组(目前只支持单个导入脚本,如果需要多个,需要下面的代码) |
| 182 | ul_element = soup.select_one("body > div.width-constraint > section > ul") |
| 183 | if ul_element: |
| 184 | li_elements = ul_element.find_all("li") |
| 185 | for li in li_elements: |
| 186 | a_tag = li.find("a") |
| 187 | if a_tag and 'href' in a_tag.attrs: |
| 188 | link = a_tag['href'] |
| 189 | match = re.search(r'/scripts/(\d+)-(.+)', link) |
| 190 | script_id = match.group(1) # ID |
| 191 | description = unquote(match.group(2)) # 名称 |
| 192 | results.append([script_id, description]) |
| 193 | return int(script_id) |
| 194 | else: |
| 195 | print("脚本返回的元素未找到,需要手动检查脚本是否被导入了.") |
| 196 | return 0 |
| 197 | else: |
| 198 | raise Exception( |
| 199 | f"导入被拒绝,状态码: {response.status_code}\n{response.text}") |
| 200 | return 0 |
| 201 | |
| 202 | def sync_update(self, script_url, script_id, attribute_default, additional_info): |
| 203 | """ |
no test coverage detected