Last active
August 25, 2023 11:43
-
-
Save dmiro/6688b92f25f9c1fedc1b7bfd22f0de68 to your computer and use it in GitHub Desktop.
Create sqlite database from repository radio-browser.info
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import subprocess | |
import gzip | |
import os | |
""" | |
create sqlite database from repository radio-browser.info | |
prerequisites: | |
(1) install sqlite3 (client) | |
> sudo apt-get install sqlite3 | |
(2) copy mysql2sqlite script from github | |
https://github.com/dumblob/mysql2sqlite | |
""" | |
# init | |
print '--init--' | |
# creating database backup | |
if os.path.exists('radio.db'): | |
print 'creating database backup' | |
os.rename('radio.db', 'radio.db.bak') | |
# download radio-browser database | |
print 'download radio-browser database' | |
subprocess.call(['curl', 'http://www.radio-browser.info/backups/latest.sql.gz', '--output', 'latest.sql.gz']) | |
# decompress database | |
print 'decompress database' | |
inF = gzip.open('latest.sql.gz', 'rb') | |
outF = open('latest.sql', 'wb') | |
outF.write(inF.read()) | |
inF.close() | |
outF.close() | |
# convert dump from mysql to sqlite | |
print 'convert dump from mysql to sqlite' | |
process = subprocess.Popen(['./mysql2sqlite', 'latest.sql'], stdout=subprocess.PIPE) | |
out, err = process.communicate() | |
outF = open('latest.sqlite', 'wb') | |
outF.write(out) | |
outF.close() | |
# create and populate database | |
print 'create and populate database' | |
myinput = open('latest.sqlite') | |
process = subprocess.Popen(['sqlite3', 'radio.db'], stdin=myinput) | |
process.wait() | |
# remove files | |
print 'removing files' | |
if os.path.exists('latest.sql.gz'): os.remove('latest.sql.gz') | |
if os.path.exists('latest.sql'): os.remove('latest.sql') | |
if os.path.exists('latest.sqlite'): os.remove('latest.sqlite') | |
# end | |
print '--end--' | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I wrote a docker-compose stack to to the same - so you don't have to install anything on your machine besides docker-compose:
https://github.com/ifischer/radio-browser-mysql-to-sqlite
It uses rubygem Sequel which seems like a much cleaner approach than mysql2sqlite and it can convert to any other database, too.