]> code.communitydata.science - taguette_google_sheet_integration.git/blob - taguette-export_tags_to_csv.py
added new code to support categories
[taguette_google_sheet_integration.git] / taguette-export_tags_to_csv.py
1 #!/usr/bin/env python3
2
3 import re
4 import json
5 import sqlite3
6 from configparser import ConfigParser
7
8 config = ConfigParser()
9 config.read('.taguette_gdocs')
10
11 ## this is project ID from the configuration
12 project_id = int(config['General']['taguette_project_id'])
13 taguette_database_file = config['General']['taguette_database_file']
14
15
16 ## connect to sqlite3
17 con = sqlite3.connect(taguette_database_file)
18 cur = con.cursor()
19
20 # Run this if you just want tags and no highlights
21 sql_stmt_get = "SELECT id, path, description FROM tags WHERE project_id = ?"
22
23 # Run this if you want tags AND highlights
24 #sql_stmt_get = "SELECT tags.id, tags.path, tags.description, highlights.snippet FROM highlight_tags INNER JOIN tags ON highlight_tags.tag_id = tags.id INNER JOIN highlights ON highlight_tags.highlight_id = highlights.id WHERE project_id = ?"
25 cur.execute(sql_stmt_get, (project_id,))
26
27 # print out a header
28 print("\t".join(['id', 'axial codes', 'tags', 'category', 'description']))
29
30 while True:
31     row = cur.fetchone()
32     if row == None:
33         break
34         
35     tag_id, path, description = row
36
37     tag_match = re.match(r'^(.+)\_(.*)$', path) 
38     if tag_match:
39         axial = tag_match.group(1)
40         tag = tag_match.group(2)
41     else:
42         axial = ""
43         tag = path
44
45     # look for extra category information stored in the description
46     cat_match = re.match('^(.*)\s*(\{(.*)\})$', description)
47     if cat_match:
48         description = cat_match.group(1)
49         category = json.loads(cat_match.group(2))["category"]
50     else:
51         category = ""
52     
53     print("\t".join([str(tag_id), axial, tag, category, description]))
54

Community Data Science Collective || Want to submit a patch?