]> code.communitydata.science - taguette_google_sheet_integration.git/blob - taguette-export_tags_to_csv.py
Added feature to read in multiple config files and iterate over each one to produce...
[taguette_google_sheet_integration.git] / taguette-export_tags_to_csv.py
1 #!/usr/bin/env python3
2
3 import re
4 import sqlite3
5 from configparser import ConfigParser
6 import csv
7 import os
8
9 config_files = [f for f in os.listdir() if f.startswith('.taguette_gdocs_')]
10
11 for file_path in config_files:
12
13     config = ConfigParser()
14     config.read(file_path)
15
16     ## this is project ID from the configuration
17     project_id = int(config['General']['taguette_project_id'])
18     taguette_database_file = config['General']['taguette_database_file']
19
20     # set output file name
21     output_file_name = f'exported_tags/exported_tags_{project_id}.tsv'
22
23     ## connect to sqlite3
24     con = sqlite3.connect(taguette_database_file)
25     cur = con.cursor()
26
27     # Run this if you just want tags and no highlights
28     sql_stmt_get = "SELECT id, path, description FROM tags WHERE project_id = ?"
29
30     # Run this if you want tags AND highlights
31     #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 = ?"
32     cur.execute(sql_stmt_get, (project_id,))
33
34
35     with open(output_file_name, 'w', newline='') as output_file:
36         writer = csv.writer(output_file, delimiter='\t')
37         while True:
38             row = cur.fetchone()
39             if row == None:
40                 break
41
42             tag_id, path, description = row
43
44             m = re.match(r'^(.+)\_(.*)$', path)
45             if m:
46                 axial = m.group(1)
47                 tag = m.group(2)
48             else:
49                 axial = ""
50                 tag = path
51
52             writer.writerow([str(tag_id), axial, tag, description])

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