Skip to main content

Python wrapper for alist.

Project description

Alist web API 的 Python 封装

安装

通过 pypi

pip install -U python-alist

使用实例

实例只提供最简单的使用例子,也没有覆盖所有方法,具体建议自己看源代码阅读理解 😂。

1. 就像在文件系统中操作

>>> # 导入模块
>>> from alist import AlistClient, AlistFileSystem
>>> # 创建客户端对象,登录 alist:此处,用户名是 "admin",密码是 "123456"
>>> client = AlistClient("http://localhost:5244", "admin", "123456")
>>> # 创建文件系统对象
>>> fs = AlistFileSystem(client)
>>> # 或者,直接用 AlistFileSystem.login 方法登录
>>> fs = AlistFileSystem.login("http://localhost:5244", "admin", "123456")
>>> # 获取当前位置
>>> fs.getcwd()
'/'
>>> # 罗列当前目录,类似 os.listdir
>>> fs.listdir()
['115', '阿里云盘']
>>> # 使用 listdir_attr 罗列当前目录,可以获取属性
>>> fs.listdir_attr()
[<alist.AlistPath(name='115', size=0, is_dir=True, modified='2023-10-23T19:54:21.483857+08:00', created='2023-10-23T19:54:21.483857+08:00', sign='', thumb='', type=1, hashinfo='null', hash_info=None, fs=alist.AlistFileSystem(client=alist.AlistClient(origin='http://localhost:5244', username=None, password='******'), path='/', refresh=False), path='/115', password='', attr_last_fetched=None)>, <alist.AlistPath(name='阿里云盘', size=0, is_dir=True, modified='2023-10-01T16:26:52.862197+08:00', created='2023-10-01T16:26:52.862197+08:00', sign='', thumb='', type=1, hashinfo='null', hash_info=None, fs=alist.AlistFileSystem(client=alist.AlistClient(origin='http://localhost:5244', username=None, password='******'), path='/', refresh=False), path='/阿里云盘', password='', attr_last_fetched=None)>]
>>> # 进入 "115" 目录
>>> fs.chdir("115")
>>> # 下面是我的 "115" 目录的罗列结果,你肯定和我不同😄
>>> fs.listdir()
['云下载', '000阅读·乱七八糟', '电视剧', '电影', '纪录片', 'libgen', '👾0号:重要资源', '📚1号:书籍大礼包', '📼资料备份']
>>> fs.chdir("电视剧/欧美剧/A")
>>> fs.getcwd()
'/115/电视剧/欧美剧/A'
>>> fs.listdir()
['A《爱、死亡和机器人》(Love.Death.and.Robot)[tt9561862]']
>>> fs.chdir("A《爱、死亡和机器人》(Love.Death.and.Robot)[tt9561862]/爱、死亡和机器人S01.Love.Death.and.Robots.1080p.NF.WEB-DL.DDP5.1.x264-NTG(18集)")
>>> fs.listdir()
['Love.Death.and.Robots.S01E01.Sonnies.Edge.1080p.NF.WEB-DL.DDP5.1.x264-NTG.简体&英文.ass', 'Love.Death.and.Robots.S01E01.Sonnies.Edge.1080p.NF.WEB-DL.DDP5.1.x264-NTG.mkv']
>>> # 查看一个文件的属性信息
>>> fs.attr("Love.Death.and.Robots.S01E01.Sonnies.Edge.1080p.NF.WEB-DL.DDP5.1.x264-NTG.mkv")
{'name': 'Love.Death.and.Robots.S01E01.Sonnies.Edge.1080p.NF.WEB-DL.DDP5.1.x264-NTG.mkv', 'size': 924544482, 'is_dir': False, 'modified': '2023-02-24T11:42:00+08:00', 'created': '2023-02-24T11:42:51+08:00', 'sign': '', 'thumb': '', 'type': 2, 'hashinfo': '{"sha1":"7F4121B68A4E467ABF30A84627E20A8978895A4E"}', 'hash_info': {'sha1': '7F4121B68A4E467ABF30A84627E20A8978895A4E'}, 'raw_url': 'http://localhost:5244/p/115/%E7%94%B5%E8%A7%86%E5%89%A7/%E6%AC%A7%E7%BE%8E%E5%89%A7/A/A%E3%80%8A%E7%88%B1%E3%80%81%E6%AD%BB%E4%BA%A1%E5%92%8C%E6%9C%BA%E5%99%A8%E4%BA%BA%E3%80%8B%28Love.Death.and.Robot%29%5Btt9561862%5D/%E7%88%B1%E3%80%81%E6%AD%BB%E4%BA%A1%E5%92%8C%E6%9C%BA%E5%99%A8%E4%BA%BAS01.Love.Death.and.Robots.1080p.NF.WEB-DL.DDP5.1.x264-NTG%EF%BC%8818%E9%9B%86%EF%BC%89/Love.Death.and.Robots.S01E01.Sonnies.Edge.1080p.NF.WEB-DL.DDP5.1.x264-NTG.mkv', 'readme': '', 'provider': '115 Cloud', 'related': [{'name': 'Love.Death.and.Robots.S01E01.Sonnies.Edge.1080p.NF.WEB-DL.DDP5.1.x264-NTG.简体&英文.ass', 'size': 48910, 'is_dir': False, 'modified': '2023-03-23T22:09:00+08:00', 'created': '2023-03-23T22:09:09+08:00', 'sign': '', 'thumb': '', 'type': 4, 'hashinfo': '{"sha1":"30AB3A1A376DE83049B35F135A774980F5C7C558"}', 'hash_info': {'sha1': '30AB3A1A376DE83049B35F135A774980F5C7C558'}}]}
>>> # 打开一个文本文件
>>> f = fs.open("Love.Death.and.Robots.S01E01.Sonnies.Edge.1080p.NF.WEB-DL.DDP5.1.x264-NTG.简体&英文.ass", encoding="UTF-16")
>>> f
<_io.TextIOWrapper name='Love.Death.and.Robots.S01E01.Sonnies.Edge.1080p.NF.WEB-DL.DDP5.1.x264-NTG.简体&英文.ass' encoding='UTF-16'>
>>> # 读取 100 个字符
>>> f.read(100)
'[Script Info]\n;SrtEdit 6.3.2012.1001\n;Copyright(C) 2005-2012 Yuan Weiguo\n\nTitle: YYeTs\nOriginal Scri'
>>> # 关闭文件(其实不主动关闭也可以,只要文件不被引用,就会自动关闭)
>>> f.close()
>>> # 打开一个二进制文件
>>> f = fs.open("Love.Death.and.Robots.S01E01.Sonnies.Edge.1080p.NF.WEB-DL.DDP5.1.x264-NTG.mkv", "rb")
>>> f
<alist.AlistFile(client=alist.AlistClient(origin='http://localhost:5244', username='admin', password='******'), path='/115/电视剧/欧美剧/A/A《爱、死亡和机器人》(Love.Death.and.Robot)[tt9561862]/爱、死亡和机器人S01.Love.Death.and.Robots.1080p.NF.WEB-DL.DDP5.1.x264-NTG(18集)/Love.Death.and.Robots.S01E01.Sonnies.Edge.1080p.NF.WEB-DL.DDP5.1.x264-NTG.mkv', mode='r') at 0x105412290>
>>> # 读取 10 字节
>>> f.read(10)
b'\x1aE\xdf\xa3\xa3B\x86\x81\x01B'
>>> # 当前文件偏移位置(从 0 开始计算)
>>> f.tell()
10
>>> f.read(10)
b'\xf7\x81\x01B\xf2\x81\x04B\xf3\x81'
>>> f.tell()
20
>>> # 把读取位置重新变为文件开头
>>> f.seek(0)
0
>>> # 再次读取 20 字节,应该等于前两次结果的拼接
>>> f.read(20)
b'\x1aE\xdf\xa3\xa3B\x86\x81\x01B\xf7\x81\x01B\xf2\x81\x04B\xf3\x81'
>>> f.tell()
20
>>> # 回到根目录
>>> fs.chdir("/")
>>> # 使用 walk,类似 os.walk
>>> next(fs.walk())
('/', ['115', '阿里云盘'], [])
>>> # 使用 walk_attr,可以获取属性
>>> next(fs.walk_attr())
('/', [<alist.AlistPath(name='115', size=0, is_dir=True, modified='2023-10-23T19:54:21.483857+08:00', created='2023-10-23T19:54:21.483857+08:00', sign='', thumb='', type=1, hashinfo='null', hash_info=None, fs=alist.AlistFileSystem(client=alist.AlistClient(origin='http://localhost:5244', username=None, password='******'), path='/', refresh=False), path='/115', password='', attr_last_fetched=None)>, <alist.AlistPath(name='阿里云盘', size=0, is_dir=True, modified='2023-10-01T16:26:52.862197+08:00', created='2023-10-01T16:26:52.862197+08:00', sign='', thumb='', type=1, hashinfo='null', hash_info=None, fs=alist.AlistFileSystem(client=alist.AlistClient(origin='http://localhost:5244', username=None, password='******'), path='/', refresh=False), path='/阿里云盘', password='', attr_last_fetched=None)>], [])
>>> # 获取当前目录下所有 .mkv 文件的 url,方法 1
>>> for path in fs.iterdir(max_depth=-1):
>>>     if path.name.endswith(".mkv"):
>>>         # 获取下载链接(要么是直链,不然就是 alist 的下载链接)
>>>         print(path.url)
http://localhost:5244/d/115/%E4%BA%91%E4%B8%8B%E8%BD%BD/A.Million.Miles.Away.2023.1080p.AMZN.WEB-DL.DDP5.1.H.264-AceMovies%5BTGx%5D/A.Million.Miles.Away.2023.1080p.AMZN.WEB-DL.DDP5.1.H.264-AceMovies.mkv
http://localhost:5244/d/115/%E4%BA%91%E4%B8%8B%E8%BD%BD/About.My.Father.2023.720p.AMZN.WEBRip.800MB.x264-GalaxyRG%5BTGx%5D/About.My.Father.2023.720p.AMZN.WEBRip.800MB.x264-GalaxyRG.mkv
...
>>> # 获取当前目录下所有 .mkv 文件的 url,方法 2
>>> for path in fs.glob("**/*.mkv"):
>>>     print(path.url)
>>> # 获取当前目录下所有 .mkv 文件的 url,方法 3
>>> for path in fs.rglob("*.mkv"):
>>>     print(path.url)

文档

正在编写,不要急 。。。

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python_alist-0.0.9.2.tar.gz (25.3 kB view details)

Uploaded Source

Built Distribution

python_alist-0.0.9.2-py3-none-any.whl (28.8 kB view details)

Uploaded Python 3

File details

Details for the file python_alist-0.0.9.2.tar.gz.

File metadata

  • Download URL: python_alist-0.0.9.2.tar.gz
  • Upload date:
  • Size: 25.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.0 CPython/3.11.4 Darwin/21.6.0

File hashes

Hashes for python_alist-0.0.9.2.tar.gz
Algorithm Hash digest
SHA256 c69a5da57060eb9495cee13fa31413bb892501a4988bc0c9850fd287d3862f37
MD5 c01ec07e1d27811ecd29acb6360cd8c0
BLAKE2b-256 3717cee8c68d7adc81d56f3e2fd3219960ff6c612346143a081a9e63cbf4c1f7

See more details on using hashes here.

File details

Details for the file python_alist-0.0.9.2-py3-none-any.whl.

File metadata

  • Download URL: python_alist-0.0.9.2-py3-none-any.whl
  • Upload date:
  • Size: 28.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.0 CPython/3.11.4 Darwin/21.6.0

File hashes

Hashes for python_alist-0.0.9.2-py3-none-any.whl
Algorithm Hash digest
SHA256 d9e48fd47ffc200c87ad340440f1debae63f57deac5d042cdaa4646cfb23ef48
MD5 f61e3aab6b5ce4508dc132bbd8f86a26
BLAKE2b-256 f482b15a743e0492d9380b44fd5441dafdee4dfb9ebba34d9ba8b4378cf71956

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page