hfmirror
Mirror for resources to local and huggingface.
Installation
You can simply install it with pip command line from the official PyPI site.
pip install hfmirror
For more information about installation, you can refer to Installation.
Quick Start
Mirror Github Releases to Your Disk
The following code can mirror the release files of repository narugo1992/gchar to your local directory test_releases
from hfmirror.resource import GithubReleaseResource
from hfmirror.storage import LocalStorage
from hfmirror.sync import SyncTask
if __name__ == '__main__':
github = GithubReleaseResource(
# the github repository
repo='narugo1992/gchar',
# access_token of github client (if needed)
access_token='my_github_access_token',
# add files like LATEST_RELEASE to mark the versions
add_version_attachment=True,
)
storage = LocalStorage('test_releases')
task = SyncTask(github, storage)
task.sync()
This is the test_releases
test_releases
├── LATEST_RELEASE
├── LATEST_RELEASE_0
├── LATEST_RELEASE_0.0
├── LATEST_RELEASE_0.0.1
├── LATEST_RELEASE_0.0.2
├── LATEST_RELEASE_0.0.3
├── LATEST_RELEASE_0.0.4
├── LATEST_RELEASE_0.0.5
├── LATEST_RELEASE_0.0.6
├── LATEST_RELEASE_0.0.8
├── v0.0.1
│ ├── gchar-0.0.1-py3-none-any.whl
│ └── gchar-0.0.1.tar.gz
├── v0.0.2
│ ├── gchar-0.0.2-py3-none-any.whl
│ └── gchar-0.0.2.tar.gz
├── v0.0.3
│ ├── gchar-0.0.3-py3-none-any.whl
│ └── gchar-0.0.3.tar.gz
├── v0.0.4
│ ├── gchar-0.0.4-py3-none-any.whl
│ └── gchar-0.0.4.tar.gz
├── v0.0.5
│ ├── gchar-0.0.5-py3-none-any.whl
│ └── gchar-0.0.5.tar.gz
├── v0.0.6
│ ├── gchar-0.0.6-py3-none-any.whl
│ └── gchar-0.0.6.tar.gz
└── v0.0.8
├── gchar-0.0.8-py3-none-any.whl
└── gchar-0.0.8.tar.gz
Mirror Game Skins to HuggingFace
Your can mirror the skins of genshin impact based on gchar to huggingface repo, using the following code with custom resource and huggingface repository storage:
import mimetypes
import os
import re
from typing import Iterable, Union, Tuple, Any, Mapping, List, Type
from gchar.games.base import Character as BaseCharacter
from gchar.games.genshin import Character
from hbutils.system import urlsplit
from huggingface_hub import HfApi
from tqdm.auto import tqdm
from hfmirror.resource import SyncResource
from hfmirror.resource.resource import TargetPathType
from hfmirror.storage import HuggingfaceStorage
from hfmirror.sync import SyncTask
from hfmirror.utils import srequest, get_requests_session
class ArknightsSkinResource(SyncResource):
def __init__(self, chs: List[Character], ch_type: Type[BaseCharacter]):
self.characters = chs
self.ch_type = ch_type
self.session = get_requests_session()
def grab(self) -> Iterable[Union[
Tuple[str, Any, TargetPathType, Mapping],
Tuple[str, Any, TargetPathType],
]]:
yield 'metadata', {'game': self.ch_type.__game_name__}, ''
_exist_ids = set()
for ch in tqdm(self.characters):
if ch.index in _exist_ids:
continue
metadata = {
'id': ch.index,
'cnname': str(ch.cnname) if ch.cnname else None,
'jpname': str(ch.jpname) if ch.jpname else None,
'enname': str(ch.enname) if ch.enname else None,
'alias': list(map(str, ch.alias_names)),
}
yield 'metadata', metadata, f'{ch.index}'
_exist_ids.add(ch.index)
for skin in ch.skins:
_, ext = os.path.splitext(urlsplit(skin.url).filename)
if not ext:
resp = srequest(self.session, 'HEAD', skin.url)
ext = mimetypes.guess_extension(resp.headers['Content-Type'])
filename = re.sub(r'\W+', '_', skin.name).strip('_') + ext
yield 'remote', skin.url, f'{ch.index}/{filename}', {'name': skin.name}
if __name__ == '__main__':
resource = ArknightsSkinResource(
Character.all(contains_extra=False),
Character
)
api = HfApi(token=os.environ['HF_TOKEN'])
api.create_repo('narugo/test_repo', repo_type='dataset', exist_ok=True)
storage = HuggingfaceStorage(
repo='narugo/test_repo',
hf_client=api,
namespace='genshin',
)
task = SyncTask(resource, storage)
task.sync()
Metadata
Release files for hfmirror 0.0.7
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| hfmirror-0.0.7.tar.gz | 24.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| hfmirror-0.0.7-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 52.7 kB
Release files / hfmirror-0.0.7.tar.gz
| Download URL | hfmirror-0.0.7.tar.gz |
|---|---|
| Size | 24.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f8f5badef1eb87655d3d5d48ea42315ce7299b661f3b34dd7016b8a0d106ce4c
|
|
BLAKE2b-256 checksum How to use checksums |
08fb3b3baa81f04bbfbb25944340300a71d7620f6a52f62ca93475672d9f5663
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.9.18
|
Release files / hfmirror-0.0.7-py3-none-any.whl
| Download URL | hfmirror-0.0.7-py3-none-any.whl |
|---|---|
| Size | 28.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
408b66bd5c20bfe321e3342f0b8f13d3db3c69e72a6620cbe3b84db09840d64c
|
|
BLAKE2b-256 checksum How to use checksums |
f64f9f7d918dc3c51b8d7b05eb9c1e33fbc44042fcbfa2e9d97b7d23b3854b72
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.9.18
|