超星云盘上传API接口

文件大小 需要cookies 上传显示进度条 占用账号网盘空间 支持秒传
小于200MB 否 否 否 否
大于200MB(目前没发现上限) 是 是 否(但是空间不够会失败) 是

  1. # -*- coding: utf-8 -*-
  2. import requests,json
  3. from ftplib import FTP
  4. import os, time, sys
  5. cookie = { #在下方引号内填入UID和uf两个cookies参数,仅上传200MB以上文件需要
  6. "UID": "",
  7. "uf": ""
  8. }
  9. class Chaopan(object):
  10. def __init__(self, cookies):
  11. """登录超盘"""
  12. session = requests.session()
  13. requests.utils.add_dict_to_cookiejar(session.cookies, cookies)
  14. response = session.get('https://pan-yz.chaoxing.com/api/token/uservalid')
  15. retobj = json.loads(response.text)
  16. if not retobj["result"]:
  17. raise Exception("参数验证失败,登录状态失效")
  18. self.__token = retobj["_token"]
  19. self.__id = cookies["UID"]
  20. self.__session = session
  21. def __get_info(self):
  22. url = f'https://pan-yz.chaoxing.com/api/info?puid={self.__id}&_token={self.__token}'
  23. return self.__session.get(url).json()["data"]
  24. def get_disk_capacity(self):
  25. """获取总空间和已用空间大小"""
  26. url = f'https://pan-yz.chaoxing.com/api/getUserDiskCapacity?puid={self.__id}&_token={self.__token}'
  27. response = self.__session.get(url)
  28. retobj = json.loads(response.text)
  29. return retobj
  30. def list_dir(self, fldid='', orderby='d', order='desc', page=1, size=100, addrec=False, showCollect=1):
  31. """列举目录文件"""
  32. url = f'https://pan-yz.chaoxing.com/api/getMyDirAndFiles?puid={self.__id}&fldid={fldid}&orderby={orderby}&order={order}&page={page}&size={size}&_token={self.__token}&addrec={addrec}&showCollect={showCollect}'
  33. response = self.__session.get(url)
  34. retobj = json.loads(response.text)
  35. return retobj
  36. def __create_file_new(self, file: "本地文件", fldid=""):
  37. url = 'https://pan-yz.chaoxing.com/opt/createfilenew'
  38. BYTES_PER_CHUNK = 512 * 1024
  39. LIMIT = 1024 * 1024
  40. ffile = []
  41. rr = file.tell()
  42. size = file.seek(0, 2)
  43. file.seek(0, 0)
  44. ffile.append(file.read(BYTES_PER_CHUNK))
  45. file.seek(BYTES_PER_CHUNK + size - LIMIT, 0)
  46. ffile.append(file.read(BYTES_PER_CHUNK))
  47. file.seek(0, rr)
  48. path,name = os.path.split(file.name)
  49. files = {
  50. "file0":(ffile[0]),
  51. "file1":(ffile[1])
  52. }
  53. post_data = {
  54. "size": size,
  55. "fn": name,
  56. "puid":0,
  57. }
  58. if fldid:
  59. post_data["fldid"] = fldid
  60. response = self.__session.post(url, data=post_data, files=files)
  61. return json.loads(response.text)
  62. def __ftp_upload_file(self, file: "本地文件", timemil, callback=None):
  63. jindu = [file.seek(0, 2),0]
  64. def __callback(block):
  65. jindu[1] += 8192
  66. if jindu[1] < jindu[0]:
  67. callback(jindu[1] / jindu[0])
  68. else:
  69. callback(1)
  70. info = self.__get_info()
  71. upath = info["froot"]
  72. host = info["host"]
  73. ftp = FTP()
  74. ftp.encoding = 'utf-8'
  75. ftp.connect(host, 21)
  76. ftp.login("usertemp", "0GYF0hBAbsXVBZCUPaSOVS")
  77. ftp.set_pasv(True)
  78. ftp.mkd(f'/{upath}/{timemil}')
  79. path,name = os.path.split(file.name)
  80. file.seek(0, 0)
  81. if callback:
  82. res = ftp.storbinary(f'STOR /{upath}/{timemil}/{name}', file, blocksize=8192, callback=__callback)
  83. else:
  84. res = ftp.storbinary(f'STOR /{upath}/{timemil}/{name}', file)
  85. ret = res.find('226') != -1
  86. ftp.quit()
  87. return ret
  88. def __sync_upload(self, timemil, pntid=""):
  89. url = 'https://pan-yz.chaoxing.com/api/notification/rsyncsucss'
  90. post_data = {
  91. "puid": self.__id,
  92. "rf": timemil,
  93. "_token": self.__token
  94. }
  95. if pntid:
  96. post_data["pntid"] = pntid
  97. response = self.__session.post(url, data=post_data)
  98. return json.loads(response.text)
  99. def __crcstatus(self, crc):
  100. url = f'https://pan-yz.chaoxing.com/api/crcstatus?puid={self.__id}&crc={crc}&_token={self.__token}'
  101. response = requests.get(url)
  102. return json.loads(response.text)
  103. def upload_file(self, file: "本地文件", fldid="", callback=None):
  104. """上传文件"""
  105. size = Chaopan.__getsize(file)
  106. if size > 1024 * 1024 + 1024 * 1024:
  107. retobj = self.__create_file_new(file, fldid)
  108. if retobj["result"]:
  109. return retobj["data"]
  110. crc = retobj["crc"]
  111. timemil = retobj["timemil"]
  112. if self.__ftp_upload_file(file, timemil, callback):
  113. self.__sync_upload(timemil, fldid)
  114. return self.__crcstatus(crc)
  115. else:
  116. timemil = int(time.time() * 1000)
  117. self.__ftp_upload_file(file, timemil, callback)
  118. return self.__sync_upload(timemil, fldid)
  119. def del_file(self, id: '文件id,多个请用英文逗号","分隔'):
  120. """删除网盘上文件"""
  121. url = 'https://pan-yz.chaoxing.com/api/delete'
  122. post_data = {
  123. "puid": self.__id,
  124. "resids": id,
  125. "_token": self.__token
  126. }
  127. response = self.__session.post(url, data=post_data)
  128. return json.loads(response.text)
  129. @staticmethod
  130. def upload_share_file(file: "本地文件或Bytes"):
  131. """上传本地文件转链接,不得大于200M"""
  132. size = Chaopan.__getsize(file)
  133. if size == 0 or size > 200000000:
  134. return {"status":False,"msg":"文件大小必须在0-200MB之间"}
  135. url = 'http://notice.chaoxing.com/pc/files/uploadNoticeFile'
  136. file_data = {
  137. 'attrFile': file
  138. }
  139. response = requests.post(url, files=file_data)
  140. return json.loads(response.text)
  141. @staticmethod
  142. def __getsize(file):
  143. """获取文件大小"""
  144. import _io
  145. if isinstance(file, _io.BufferedReader):
  146. rr = file.tell()
  147. size = file.seek(0, 2)
  148. file.seek(0, rr)
  149. return size
  150. elif isinstance(file, bytes):
  151. return len(file)
  152. else:
  153. return 0
  154. def callback(per):
  155. hashes = '#' * int(per * 30)
  156. spaces = ' ' * (30 - len(hashes))
  157. sys.stdout.write("\rPercent: [%s] %d%%"%(hashes + spaces, per*100))
  158. sys.stdout.flush()
  159. filepath = input("请输入文件路径: ")
  160. try:
  161. with open(filepath, 'rb') as f:
  162. size = f.seek(0, 2)
  163. f.seek(0, 0)
  164. if size <= 200000000:
  165. print("正在上传中,请等待...")
  166. ret = Chaopan.upload_share_file(f)
  167. if ret["status"]:
  168. print(f'下载直链为(http替换为https仍然可用): {ret["att_file"]["att_clouddisk"]["downPath"]}')
  169. print(f'分享链接为: {ret["att_file"]["att_clouddisk"]["shareUrl"]}')
  170. else:
  171. print(f'转直链失败,原因为{ret["msg"]}')
  172. else:
  173. print("您的文件大于200M, 正在以登录状态上传,请等待...")
  174. cp = Chaopan(cookie)
  175. retobj = cp.upload_file(f, callback=callback)
  176. print("")
  177. print(f'下载直链为(http替换为https仍然可用): http://d0.ananas.chaoxing.com/download/{retobj["objectId"]}')
  178. print(f'分享链接为: http://cloud.ananas.chaoxing.com/view/fileview?objectid={retobj["objectId"]}')
  179. print('正在清理网盘空间(清理后上传的文件不会占用您的网盘空间)')
  180. time.sleep(3)
  181. if "id" in retobj:
  182. cp.del_file(retobj["id"])
  183. elif "resid" in retobj:
  184. cp.del_file(retobj["resid"])
  185. print('清理完成')
  186. except Exception as e:
  187. print(f'出现错误,原因为:{str(e)}')
点赞