Skip to content

Commit

Permalink
feat: add CLI command to download and cache the RB6 db key from the w…
Browse files Browse the repository at this point in the history
…eb (#64)

Pyrekordbox tries to download the key from projects that have hard-coded the key (see issue #77). If the download was successful it writes it to the cache file.
  • Loading branch information
dylanljones committed Aug 15, 2023
1 parent 82b6ac7 commit 38d2278
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,13 @@ from pyrekordbox import Rekordbox6Database
write_db6_key_cache("<insert key here>") # call once
db = Rekordbox6Database()
````
The command line interface of ``pyrekordbox`` also
provides a command for downloading the key from known sources and writing it to the
cache file:
````shell
> python -m pyrekordbox download-key
````


## 💡 File formats

Expand Down
6 changes: 6 additions & 0 deletions docs/source/tutorial/db6.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ from pyrekordbox import Rekordbox6Database
write_db6_key_cache("<insert key here>") # call once
db = Rekordbox6Database()
````
The command line interface of ``pyrekordbox`` also
provides a command for downloading the key from known sources and writing it to the
cache file:
````shell
> python -m pyrekordbox download-key
````



Expand Down
52 changes: 51 additions & 1 deletion pyrekordbox/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,29 @@
# Date: 2023-08-15

import os
import re
import sys
import shutil
import urllib.request
from pyrekordbox.config import write_db6_key_cache, _cache_file

# fmt: off
KEY_SOURCES = [
{
"url": r"https://raw.githubusercontent.com/mganss/CueGen/19878e6eb3f586dee0eb3eb4f2ce3ef18309de9d/CueGen/Generator.cs", # noqa: E501
"regex": re.compile(
r'((.|\n)*)Config\.UseSqlCipher.*\?.*"(?P<dp>.*)".*:.*null',
flags=re.IGNORECASE | re.MULTILINE
)
},
{
"url": r"https://raw.githubusercontent.com/dvcrn/go-rekordbox/8be6191ba198ed7abd4ad6406d177ed7b4f749b5/cmd/getencryptionkey/main.go", # noqa: E501
"regex": re.compile(
r'((.|\n)*)fmt\.Print\("(?P<dp>.*)"\)',
flags=re.IGNORECASE | re.MULTILINE)
}
]
# fmt: on


class WorkingDir:
Expand Down Expand Up @@ -124,12 +145,39 @@ def install_pysqlcipher(
print(f"Could not remove temporary directory '{tmpdir}'!")


def download_db6_key():
dp = ""
for source in KEY_SOURCES[1:]:
url = source["url"]
regex = source["regex"]
print(f"Looking for key: {url}")

res = urllib.request.urlopen(url)
data = res.read().decode("utf-8")
match = regex.match(data)
if match:
dp = match.group("dp")
break
if dp:
print(f"Found key, updating cache file {_cache_file}")
write_db6_key_cache(dp)
else:
print("No key found in the online sources.")


def main():
from argparse import ArgumentParser

parser = ArgumentParser("pyrekordbox")
subparsers = parser.add_subparsers(dest="command")

# Download Rekordbx 6 database key command
subparsers.add_parser(
"download-key",
help="Download the Rekordbox 6 database key from the internet "
"and write it to the cache file.",
)

# Install pysqlcipher3 command (Windows only)
install_parser = subparsers.add_parser(
"install-sqlcipher",
Expand Down Expand Up @@ -164,7 +212,9 @@ def main():

# Parse args and handle command
args = parser.parse_args(sys.argv[1:])
if args.command == "install-sqlcipher":
if args.command == "download-key":
download_db6_key()
elif args.command == "install-sqlcipher":
if sys.platform == "win32":
install_pysqlcipher(
args.tmpdir, args.cryptolib, args.fixquote, install=not args.buildonly
Expand Down

0 comments on commit 38d2278

Please sign in to comment.