1#!/usr/bin/env python 2# 3# Scrape GATT UUIDs from Bluetooth SIG page 4# Copyright 2016 BlueKitchen GmbH 5# 6 7from lxml import html 8import datetime 9import requests 10import sys 11import os 12 13program_info = ''' 14BTstack GATT UUID Scraper for BTstack 15Copyright 2016, BlueKitchen GmbH 16''' 17 18header = ''' 19/** 20 * bluetooth_gatt.h generated from Bluetooth SIG website for BTstack 21 */ 22 23#ifndef __BLUETOOTH_GATT_H 24#define __BLUETOOTH_GATT_H 25''' 26 27page_info = ''' 28/** 29 * Assigned numbers from {page} 30 */ 31''' 32 33trailer = ''' 34#endif 35''' 36 37def scrape_page(fout, url): 38 print("Parsing %s" % url) 39 fout.write(page_info.format(page=url)) 40 page = requests.get(url) 41 tree = html.fromstring(page.content) 42 # get all <tr> elements in <table id="gattTable"> 43 rows = tree.xpath('//table[@id="gattTable"]/tbody/tr') 44 for row in rows: 45 children = row.getchildren() 46 summary = children[0].text_content() 47 id = children[1].text_content() 48 uuid = children[2].text_content() 49 if (len(id)): 50 tag = id.upper().replace('.', '_').replace('-','_') 51 fout.write("#define %-80s %s // %s\n" % (tag, uuid, summary)) 52 53btstack_root = os.path.abspath(os.path.dirname(sys.argv[0]) + '/..') 54gen_path = btstack_root + '/src/bluetooth_gatt.h' 55 56print(program_info) 57 58with open(gen_path, 'wt') as fout: 59 fout.write(header.format(datetime=str(datetime.datetime.now()))) 60 scrape_page(fout, 'https://www.bluetooth.com/specifications/gatt/declarations') 61 scrape_page(fout, 'https://www.bluetooth.com/specifications/gatt/services') 62 scrape_page(fout, 'https://www.bluetooth.com/specifications/gatt/characteristics') 63 scrape_page(fout, 'https://www.bluetooth.com/specifications/gatt/descriptors') 64 fout.write(trailer) 65 66print('Scraping successful!\n')