Skip to content

Instantly share code, notes, and snippets.

@hadleyrich
Created November 4, 2015 05:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hadleyrich/5230b4f74dca70fbd980 to your computer and use it in GitHub Desktop.
Save hadleyrich/5230b4f74dca70fbd980 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
"""
Quick script to grab the Gladfield Beersmith XML file and insert it directly into
the Brewtarget SQLite database. You need to close Brewtarget before running.
Only tested on Linux, has the potential to work on other platforms with minor changes.
will break when Gladfield update their XML file URL.
By Hadley Rich <hads@nice.net.nz> 2015-11-04
"""
import os
import sys
import urllib
import sqlite3
import zipfile
import lxml.html
url = 'http://www.gladfieldmalt.co.nz/wp-content/uploads/Gladfield-Malts-Update-11-08-153.zip'
database = os.path.expanduser('~/.config/brewtarget/database.sqlite')
class Opener(urllib.FancyURLopener):
version = 'Mozilla/5.0 (Windows NT 6.0) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11'
urllib._urlopener = Opener()
conn = sqlite3.connect(database)
conn.row_factory = sqlite3.Row
cur = conn.cursor()
f, _ = urllib.urlretrieve(url)
zip_file = zipfile.ZipFile(open(f))
xml_file = zip_file.open(zip_file.namelist()[0])
tree = lxml.html.parse(xml_file)
for g in tree.xpath('//grain'):
out = {
'ftype': 'Grain',
'is_mashed': 1
}
for c in g.getchildren():
if c.tag == 'f_g_name':
print c.text
out['name'] = c.text
if c.tag == 'f_g_origin':
out['origin'] = c.text
if c.tag == 'f_g_supplier':
out['supplier'] = c.text
if c.tag == 'f_g_amount':
out['amount'] = c.text
if c.tag == 'f_g_color':
out['color'] = c.text
if c.tag == 'f_g_yield':
out['yield'] = c.text
if c.tag == 'f_g_coarse_fine_diff':
out['coarse_fine_diff'] = c.text
if c.tag == 'f_g_moisture':
out['moisture'] = c.text
if c.tag == 'f_g_diastatic_power':
out['diastatic_power'] = c.text
if c.tag == 'f_g_protein':
out['protein'] = c.text
if c.tag == 'f_g_add_after_boil':
out['add_after_boil'] = c.text
if c.tag == 'f_g_recommend_mash':
out['recommend_mash'] = c.text
if c.tag == 'f_g_max_in_batch':
out['max_in_batch'] = c.text
if c.tag == 'f_g_notes':
out['notes'] = c.text
query = 'INSERT INTO fermentable(%s) VALUES(:%s)' % (', '.join(out.keys()), ', :'.join(out.keys()))
cur.execute(query, out)
conn.commit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment