|
| 1 | +#!/usr/local/bin/python3 |
| 2 | +#coding: utf-8 |
| 3 | +import requests |
| 4 | +import json |
| 5 | +import sys |
| 6 | +import sqlite3 |
| 7 | +import re |
| 8 | +import codecs |
| 9 | +import os |
| 10 | +from time import sleep |
| 11 | + |
| 12 | +class SharedLinksDB: |
| 13 | + def __init__(self, db_file): |
| 14 | + self.conn = self.create_connection(db_file) |
| 15 | + if self.conn is not None: |
| 16 | + self.create_table('''CREATE TABLE IF NOT EXISTS shared_link |
| 17 | + (id INTEGER PRIMARY KEY AUTOINCREMENT, |
| 18 | + share_code TEXT, |
| 19 | + receive_code TEXT, |
| 20 | + snap_id INTEGER, |
| 21 | + file_size INTEGER, |
| 22 | + share_title TEXT, |
| 23 | + share_state INTEGER, |
| 24 | + forbid_reason INTEGER, |
| 25 | + create_time INTEGER, |
| 26 | + receive_count INTEGER, |
| 27 | + expire_time INTEGER, |
| 28 | + file_category INTEGER, |
| 29 | + auto_renewal INTEGER, |
| 30 | + auto_fill_recvcode INTEGER, |
| 31 | + can_report INTEGER, |
| 32 | + can_notice INTEGER, |
| 33 | + have_vio_file INTEGER, |
| 34 | + status INTEGER)''') |
| 35 | + |
| 36 | + self.create_table('''CREATE TABLE IF NOT EXISTS saved_data |
| 37 | + (id INTEGER PRIMARY KEY AUTOINCREMENT, |
| 38 | + n TEXT, |
| 39 | + cid TEXT)''') |
| 40 | + |
| 41 | + def __del__(self): |
| 42 | + self.close_connection() |
| 43 | + |
| 44 | + # 创建数据库连接 |
| 45 | + def create_connection(self, db_file): |
| 46 | + conn = None |
| 47 | + try: |
| 48 | + conn = sqlite3.connect(db_file) |
| 49 | + return conn |
| 50 | + except: |
| 51 | + print("[x] 无法创建数据库连接") |
| 52 | + return conn |
| 53 | + |
| 54 | + # 创建数据表 |
| 55 | + def create_table(self, create_table_sql): |
| 56 | + try: |
| 57 | + cursor = self.conn.cursor() |
| 58 | + cursor.execute(create_table_sql) |
| 59 | + except: |
| 60 | + print("[x] 无法创建数据表") |
| 61 | + |
| 62 | + # 插入分享链接信息 |
| 63 | + def insert_shared_link(self, share_code, receive_code,share_info, status): |
| 64 | + sql = '''INSERT INTO shared_link (share_code, receive_code, snap_id, file_size, share_title, share_state, forbid_reason, create_time, receive_count, expire_time, file_category, auto_renewal, auto_fill_recvcode, can_report, can_notice, have_vio_file, status)''' |
| 65 | + sql += '''VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)''' |
| 66 | + cursor = self.conn.cursor() |
| 67 | + if share_info: |
| 68 | + cursor.execute(sql, (share_code, receive_code, share_info['snap_id'], share_info['file_size'], share_info['share_title'], share_info['share_state'], share_info['forbid_reason'], share_info['create_time'], share_info['receive_count'], share_info['expire_time'], share_info['file_category'], share_info['auto_renewal'], share_info['auto_fill_recvcode'], share_info['can_report'], share_info['can_notice'], share_info['have_vio_file'], status)) |
| 69 | + else: |
| 70 | + cursor.execute(sql, (share_code, receive_code, 0, 0, '', 0, '', 0, 0, 0, 0, 0, 0, 0, 0, 0, status)) |
| 71 | + self.conn.commit() |
| 72 | + |
| 73 | + # 检查分享链接信息是否存在 |
| 74 | + def check_shared_link(self, share_code, receive_code): |
| 75 | + sql = '''SELECT * FROM shared_link WHERE share_code = ? AND receive_code = ?''' |
| 76 | + cursor = self.conn.cursor() |
| 77 | + cursor.execute(sql, (share_code, receive_code)) |
| 78 | + rows = cursor.fetchall() |
| 79 | + return len(rows) > 0 |
| 80 | + |
| 81 | + # 插入data_list中的n值和cid值 |
| 82 | + def insert_saved_data(self, data_list): |
| 83 | + cursor = self.conn.cursor() |
| 84 | + for i, data in enumerate(data_list): |
| 85 | + sql = '''INSERT INTO saved_data (n, cid) |
| 86 | + VALUES (?, ?)''' |
| 87 | + cursor.execute(sql, (str(data['n']), data['cid'])) |
| 88 | + self.conn.commit() |
| 89 | + |
| 90 | + # 检查saved_data表中是否已经存在n值 |
| 91 | + def check_saved_data(self, n): |
| 92 | + sql = '''SELECT * FROM saved_data WHERE n = ?''' |
| 93 | + cursor = self.conn.cursor() |
| 94 | + cursor.execute(sql, (n,)) |
| 95 | + rows = cursor.fetchall() |
| 96 | + return len(rows) > 0 |
| 97 | + |
| 98 | + # 关闭数据库连接 |
| 99 | + def close_connection(self): |
| 100 | + if self.conn is not None: |
| 101 | + self.conn.close() |
| 102 | + |
| 103 | +class Fake115Client(object): |
| 104 | + |
| 105 | + def __init__(self, cookie): |
| 106 | + self.cookie = cookie |
| 107 | + self.ua = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36' |
| 108 | + self.content_type = 'application/x-www-form-urlencoded' |
| 109 | + self.header = {"User-Agent": self.ua, |
| 110 | + "Content-Type": self.content_type, "Cookie": self.cookie} |
| 111 | + self.db = SharedLinksDB('shared_links.db') |
| 112 | + self.target_dir_cid = TARGETDIRCID |
| 113 | + self.get_userid() |
| 114 | + |
| 115 | + |
| 116 | + # 获取UID |
| 117 | + def get_userid(self): |
| 118 | + try: |
| 119 | + self.user_id = '' |
| 120 | + url = "https://my.115.com/?ct=ajax&ac=get_user_aq" ############ |
| 121 | + p = requests.get(url, headers=self.header) |
| 122 | + if p: |
| 123 | + rootobject = p.json() |
| 124 | + if not rootobject.get("state"): |
| 125 | + self.err = "[x] 获取 UID 错误:{}".format(rootobject.get("error_msg")) |
| 126 | + return False |
| 127 | + self.user_id = rootobject.get("data").get("uid") |
| 128 | + return True |
| 129 | + except Exception as result: |
| 130 | + print("[x] 异常错误:{}".format(result)) |
| 131 | + return False |
| 132 | + |
| 133 | + def request_datalist(self,share_code,receive_code): |
| 134 | + url = f"https://webapi.115.com/share/snap?share_code={share_code}&offset=0&limit=20&receive_code={receive_code}&cid=" |
| 135 | + data_list = [] |
| 136 | + share_info = {} |
| 137 | + try: |
| 138 | + response = requests.get(url, headers=self.header) |
| 139 | + response_json = json.loads(response.content.decode()) |
| 140 | + share_info = response_json['data'].get('shareinfo') |
| 141 | + if response_json['state'] == False: |
| 142 | + print('error:',response_json['error']) |
| 143 | + return share_info,[] |
| 144 | + count = response_json['data']['count'] |
| 145 | + data_list.extend(response_json['data']['list']) |
| 146 | + while len(data_list) < count: |
| 147 | + offset = len(data_list) |
| 148 | + response = requests.get(f"{url}&offset={offset}") |
| 149 | + response_json = json.loads(response.content.decode()) |
| 150 | + data_list.extend(response_json['data']['list']) |
| 151 | + except: |
| 152 | + data_list = [] |
| 153 | + return share_info,data_list |
| 154 | + |
| 155 | + def post_save(self, share_code, receive_code, file_ids,pid=''): |
| 156 | + print('[√]正在转存 %s:%s 中的 %d 项' % (share_code,receive_code, len(file_ids))) |
| 157 | + # 将 file_ids 用逗号拼接为一个字符串 |
| 158 | + file_id_str = ','.join(file_ids) |
| 159 | + # 构造 POST 请求的 payload |
| 160 | + if pid == '': |
| 161 | + payload = { |
| 162 | + 'user_id': self.user_id, |
| 163 | + 'share_code': share_code, |
| 164 | + 'receive_code': receive_code, |
| 165 | + 'file_id': file_id_str |
| 166 | + } |
| 167 | + else: |
| 168 | + payload = { |
| 169 | + 'user_id': self.user_id, |
| 170 | + 'share_code': share_code, |
| 171 | + 'receive_code': receive_code, |
| 172 | + 'file_id': file_id_str, |
| 173 | + 'cid': pid |
| 174 | + } |
| 175 | + # 发送 POST 请求 |
| 176 | + try: |
| 177 | + response = requests.post('https://webapi.115.com/share/receive', data=payload, headers=self.header) |
| 178 | + except: |
| 179 | + sleep(5) |
| 180 | + response = requests.post('https://webapi.115.com/share/receive', data=payload, headers=self.header) |
| 181 | + # 解析响应的 JSON 数据 |
| 182 | + result = response.json() |
| 183 | + # 判断转存是否成功 |
| 184 | + if result['state']: |
| 185 | + print('[√]转存 %s:%s 成功' % (share_code,receive_code)) |
| 186 | + return True |
| 187 | + else: |
| 188 | + print('[x]转存 %s:%s 失败,原因:%s' % (share_code,receive_code,result['error'])) |
| 189 | + return False |
| 190 | + |
| 191 | + def create_dir(self,cname): |
| 192 | + ''' |
| 193 | + pid:父目录id |
| 194 | + cname:目录名 |
| 195 | + ''' |
| 196 | + if not cname: |
| 197 | + return self.target_dir_cid |
| 198 | + data = {'pid': self.target_dir_cid,'cname':cname} |
| 199 | + try: |
| 200 | + response=requests.post('http://web.api.115.com/files/add',data=data,headers=self.header) |
| 201 | + data=response.json() |
| 202 | + if data['state']: |
| 203 | + return data['cid'] |
| 204 | + else: |
| 205 | + print('[x]'+'新建文件夹失败,错误信息:'+data['error']) |
| 206 | + return self.target_dir_cid |
| 207 | + except: |
| 208 | + print('[x]'+'新建文件夹失败') |
| 209 | + return self.target_dir_cid |
| 210 | + |
| 211 | + |
| 212 | + def save_by_sr(self,share_code, receive_code): |
| 213 | + # 检查数据库中是否存在share_code, receive_code |
| 214 | + if self.db.check_shared_link(share_code, receive_code): |
| 215 | + print('[x]已转存过 %s:%s ' % (share_code,receive_code)) |
| 216 | + return |
| 217 | + # 获取data_list |
| 218 | + share_info,data_list = self.request_datalist(share_code, receive_code) |
| 219 | + # 总数0就存share_code, receive_code到数据库 非0发送转存请求 |
| 220 | + if len(data_list) == 0: |
| 221 | + self.db.insert_shared_link(share_code, receive_code,share_info,0) |
| 222 | + return |
| 223 | + else: |
| 224 | + self.db.insert_shared_link(share_code, receive_code,share_info,1) |
| 225 | + file_ids = [] |
| 226 | + for data in data_list: |
| 227 | + n = data['n'] |
| 228 | + cid = data['cid'] |
| 229 | + fid = data.get('fid') |
| 230 | + if fid: |
| 231 | + cid = fid |
| 232 | + # 检查数据库中是否存在n值 |
| 233 | + if self.db.check_saved_data(n): |
| 234 | + print('[x]转存 %s:%s 中的 %s 已存在' % (share_code,receive_code, n)) |
| 235 | + continue |
| 236 | + file_ids.append(cid) |
| 237 | + # 发送转存请求 |
| 238 | + if len(data_list) == 1: |
| 239 | + pid = '' |
| 240 | + else: |
| 241 | + pid = self.create_dir(share_info['share_title']) |
| 242 | + if self.post_save(share_code, receive_code, file_ids,pid): |
| 243 | + self.db.insert_saved_data(data_list) |
| 244 | + |
| 245 | + |
| 246 | + def save_link(self,link): |
| 247 | + match = re.search(r's/(\w+)\?password=(\w+)', link) |
| 248 | + if match: |
| 249 | + share_code=match.group(1) |
| 250 | + receive_code=match.group(2) |
| 251 | + self.save_by_sr(share_code,receive_code) |
| 252 | + |
| 253 | + def save_link_from_file(self,filepath): |
| 254 | + with open(filepath, 'r',encoding='utf-8') as f: |
| 255 | + links = f.readlines() |
| 256 | + share_codes = [] |
| 257 | + receive_codes = [] |
| 258 | + for link in links: |
| 259 | + match = re.search(r's/(\w+)\?password=(\w+)', link) |
| 260 | + if match: |
| 261 | + share_codes.append(match.group(1)) |
| 262 | + receive_codes.append(match.group(2)) |
| 263 | + for share_code, receive_code in zip(share_codes, receive_codes): |
| 264 | + self.save_by_sr(share_code, receive_code) |
| 265 | + |
| 266 | + # 115分享链接批量转存 |
| 267 | + |
| 268 | + def save_link_from_rawfile(self,filepath): |
| 269 | + with open(filepath, 'r', encoding='utf-8') as f: |
| 270 | + lines = f.readlines() |
| 271 | + for i, line in enumerate(lines): |
| 272 | + if 'https://115.com/s/' in line: |
| 273 | + if '?password=' in line: |
| 274 | + start_index = line.find('https://115.com/s/') |
| 275 | + end_index = line.find('?password=') + 14 |
| 276 | + link = line[start_index:end_index] |
| 277 | + self.save_link(link) |
| 278 | + else: |
| 279 | + found = False |
| 280 | + start_index = line.find('https://115.com/s/') |
| 281 | + end_index = start_index + 29 |
| 282 | + link = line[start_index:end_index] |
| 283 | + |
| 284 | + for j in range(i, min(i + 6, len(lines))): |
| 285 | + if '访问码:' in lines[j]: |
| 286 | + password = re.search(r'访问码:([\d\w]{4})', lines[j]).group(1) |
| 287 | + self.save_link(link + '?password=' + password) |
| 288 | + found = True |
| 289 | + break |
| 290 | + if not found: |
| 291 | + backward = False |
| 292 | + for j in range(max(0, i - 5), i): |
| 293 | + if '访问码:' in lines[j]: |
| 294 | + password = re.search(r'访问码:([\d\w]{4})', lines[j]).group(1) |
| 295 | + new_link = link + '?password=' + password |
| 296 | + if self.save_link(new_link): |
| 297 | + break |
| 298 | + else: |
| 299 | + backward = True |
| 300 | + if backward: |
| 301 | + for j in range(max(0, i - 5), i): |
| 302 | + if '访问码:' in lines[j]: |
| 303 | + password = re.search(r'访问码:([\d\w]{4})', lines[j]).group(1) |
| 304 | + new_link = link + '?password=' + password |
| 305 | + if self.save_link(new_link): |
| 306 | + break |
| 307 | + def save_link_from_rawfiles(self,folder_path): |
| 308 | + txt_files = [] |
| 309 | + for file_name in os.listdir(folder_path): |
| 310 | + if file_name.endswith('.txt'): |
| 311 | + txt_files.append(os.path.join(folder_path, file_name)) |
| 312 | + for file_path in txt_files: |
| 313 | + self.save_link_from_rawfile(file_path) |
| 314 | + |
| 315 | + |
| 316 | + |
| 317 | +if __name__ == '__main__': |
| 318 | + # 获取cookie.txt下的COOKIE |
| 319 | + if os.path.exists('./cookie.txt'): |
| 320 | + with open('./cookie.txt', 'r') as f: |
| 321 | + COOKIE = f.read() |
| 322 | + else: |
| 323 | + print('[x]请在当前目录下创建cookie.txt文件,并将115网页版的cookie粘贴进去') |
| 324 | + # 获取要转存到的文件夹的cid |
| 325 | + if os.path.exists('./cid.txt'): |
| 326 | + with open('./cid.txt', 'r') as f: |
| 327 | + TARGETDIRCID = f.read() |
| 328 | + else: |
| 329 | + print('[x]请在当前目录下创建cid.txt文件,并将要转存到的文件夹的cid粘贴进去') |
| 330 | + |
| 331 | + |
| 332 | + if os.path.exists('./links'): |
| 333 | + if COOKIE and TARGETDIRCID: |
| 334 | + cli = Fake115Client(COOKIE) |
| 335 | + cli.save_link_from_rawfiles('./links/') |
| 336 | + else: |
| 337 | + print('[x]请检查cookie.txt和cid.txt文件') |
| 338 | + else: |
| 339 | + print('[x]已在当前目录下创建links文件夹,请将115网页版的分享链接粘贴进去,注意用utf-8编码') |
| 340 | + os.makedirs('./links') |
| 341 | + |
| 342 | + |
0 commit comments