Mercurial > code > home > repos > video
annotate video_file_store.py @ 37:7cacfae58430
thumbnails rewrite - store in db; don't use YT-provided pics for now
author | drewp@bigasterisk.com |
---|---|
date | Tue, 03 Dec 2024 19:28:11 -0800 |
parents | ed16fdbb3996 |
children | 0aea9e55899b |
rev | line source |
---|---|
15 | 1 import asyncio |
2 import hashlib | |
36
ed16fdbb3996
rewrite WIP. scan fs separately; store in db. thumbs are broken for now
drewp@bigasterisk.com
parents:
20
diff
changeset
|
3 import logging |
20 | 4 import os |
15 | 5 from dataclasses import dataclass |
6 from pathlib import Path | |
36
ed16fdbb3996
rewrite WIP. scan fs separately; store in db. thumbs are broken for now
drewp@bigasterisk.com
parents:
20
diff
changeset
|
7 from typing import Iterable, Iterator |
15 | 8 |
36
ed16fdbb3996
rewrite WIP. scan fs separately; store in db. thumbs are broken for now
drewp@bigasterisk.com
parents:
20
diff
changeset
|
9 import pymongo.collection |
ed16fdbb3996
rewrite WIP. scan fs separately; store in db. thumbs are broken for now
drewp@bigasterisk.com
parents:
20
diff
changeset
|
10 |
ed16fdbb3996
rewrite WIP. scan fs separately; store in db. thumbs are broken for now
drewp@bigasterisk.com
parents:
20
diff
changeset
|
11 log = logging.getLogger('vfs') |
20 | 12 |
15 | 13 |
14 @dataclass | |
15 class VideoFile: | |
16 diskPath: Path | |
17 webRelPath: str | |
36
ed16fdbb3996
rewrite WIP. scan fs separately; store in db. thumbs are broken for now
drewp@bigasterisk.com
parents:
20
diff
changeset
|
18 webDataPath: str |
15 | 19 label: str |
20 # perms, playlists, req by/when | |
21 | |
22 | |
23 @dataclass | |
24 class VideoFileStore: | |
36
ed16fdbb3996
rewrite WIP. scan fs separately; store in db. thumbs are broken for now
drewp@bigasterisk.com
parents:
20
diff
changeset
|
25 fs: pymongo.collection.Collection |
15 | 26 |
27 def findInDir(self, subdir: str) -> Iterable[VideoFile]: | |
36
ed16fdbb3996
rewrite WIP. scan fs separately; store in db. thumbs are broken for now
drewp@bigasterisk.com
parents:
20
diff
changeset
|
28 webRelParent = '.' if subdir == '/' else subdir |
ed16fdbb3996
rewrite WIP. scan fs separately; store in db. thumbs are broken for now
drewp@bigasterisk.com
parents:
20
diff
changeset
|
29 for doc in self.fs.find({ |
ed16fdbb3996
rewrite WIP. scan fs separately; store in db. thumbs are broken for now
drewp@bigasterisk.com
parents:
20
diff
changeset
|
30 'type': 'file', |
ed16fdbb3996
rewrite WIP. scan fs separately; store in db. thumbs are broken for now
drewp@bigasterisk.com
parents:
20
diff
changeset
|
31 'webRelParent': webRelParent |
37
7cacfae58430
thumbnails rewrite - store in db; don't use YT-provided pics for now
drewp@bigasterisk.com
parents:
36
diff
changeset
|
32 }, sort=[('label', 1)]): |
36
ed16fdbb3996
rewrite WIP. scan fs separately; store in db. thumbs are broken for now
drewp@bigasterisk.com
parents:
20
diff
changeset
|
33 yield VideoFile(Path(doc['diskPath']), doc['webRelPath'], |
ed16fdbb3996
rewrite WIP. scan fs separately; store in db. thumbs are broken for now
drewp@bigasterisk.com
parents:
20
diff
changeset
|
34 doc['webDataPath'], doc['label']) |
15 | 35 |
36 def findSubdirs(self, subdir: str) -> Iterable: | |
36
ed16fdbb3996
rewrite WIP. scan fs separately; store in db. thumbs are broken for now
drewp@bigasterisk.com
parents:
20
diff
changeset
|
37 for doc in self.fs.find({ |
ed16fdbb3996
rewrite WIP. scan fs separately; store in db. thumbs are broken for now
drewp@bigasterisk.com
parents:
20
diff
changeset
|
38 'type': |
ed16fdbb3996
rewrite WIP. scan fs separately; store in db. thumbs are broken for now
drewp@bigasterisk.com
parents:
20
diff
changeset
|
39 'dir', |
ed16fdbb3996
rewrite WIP. scan fs separately; store in db. thumbs are broken for now
drewp@bigasterisk.com
parents:
20
diff
changeset
|
40 'webRelParent': |
ed16fdbb3996
rewrite WIP. scan fs separately; store in db. thumbs are broken for now
drewp@bigasterisk.com
parents:
20
diff
changeset
|
41 '.' if subdir == '/' else subdir |
37
7cacfae58430
thumbnails rewrite - store in db; don't use YT-provided pics for now
drewp@bigasterisk.com
parents:
36
diff
changeset
|
42 }, sort=[('label', 1)]): |
36
ed16fdbb3996
rewrite WIP. scan fs separately; store in db. thumbs are broken for now
drewp@bigasterisk.com
parents:
20
diff
changeset
|
43 yield { |
ed16fdbb3996
rewrite WIP. scan fs separately; store in db. thumbs are broken for now
drewp@bigasterisk.com
parents:
20
diff
changeset
|
44 'label': doc['label'], |
ed16fdbb3996
rewrite WIP. scan fs separately; store in db. thumbs are broken for now
drewp@bigasterisk.com
parents:
20
diff
changeset
|
45 'path': doc['webRelPath'], |
ed16fdbb3996
rewrite WIP. scan fs separately; store in db. thumbs are broken for now
drewp@bigasterisk.com
parents:
20
diff
changeset
|
46 } |
15 | 47 |
48 async def save(self, name: str, chunks: Iterator[bytes]): | |
36
ed16fdbb3996
rewrite WIP. scan fs separately; store in db. thumbs are broken for now
drewp@bigasterisk.com
parents:
20
diff
changeset
|
49 raise |
15 | 50 p = self.top / name |
51 if p.exists(): | |
52 raise ValueError(f'{p} exists') | |
53 data = b'' | |
54 for c in chunks: | |
55 data += c | |
56 p.write_bytes(data) | |
20 | 57 |
58 def folderTree(self): | |
59 out = {'name': 'TOP'} | |
60 | |
61 def fill(node: dict, pathToHere: Path): | |
62 for subName in sorted(os.listdir(pathToHere)): | |
63 if subName in IGNORE: | |
64 continue | |
65 subDir = pathToHere / subName | |
66 if subDir.is_dir(): | |
67 subNode = {'name': subName} | |
68 node.setdefault('children', []).append(subNode) | |
69 fill(subNode, subDir) | |
70 | |
71 fill(out, self.top) | |
36
ed16fdbb3996
rewrite WIP. scan fs separately; store in db. thumbs are broken for now
drewp@bigasterisk.com
parents:
20
diff
changeset
|
72 return out |