Daniel@0
|
1 # Part of DML (Digital Music Laboratory)
|
Daniel@0
|
2 # Copyright 2014-2015 Daniel Wolff, City University
|
Daniel@0
|
3
|
Daniel@0
|
4 # This program is free software; you can redistribute it and/or
|
Daniel@0
|
5 # modify it under the terms of the GNU General Public License
|
Daniel@0
|
6 # as published by the Free Software Foundation; either version 2
|
Daniel@0
|
7 # of the License, or (at your option) any later version.
|
Daniel@0
|
8 #
|
Daniel@0
|
9 # This program is distributed in the hope that it will be useful,
|
Daniel@0
|
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Daniel@0
|
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Daniel@0
|
12 # GNU General Public License for more details.
|
Daniel@0
|
13 #
|
Daniel@0
|
14 # You should have received a copy of the GNU General Public
|
Daniel@0
|
15 # License along with this library; if not, write to the Free Software
|
Daniel@0
|
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
Daniel@0
|
17
|
Daniel@0
|
18 #!/usr/bin/python
|
Daniel@0
|
19 # -*- coding: utf-8 -*-
|
Daniel@0
|
20
|
Daniel@0
|
21 # this file converts data (csv) to the final web api json format.
|
Daniel@0
|
22
|
Daniel@0
|
23 __author__="Daniel Wolff"
|
Daniel@0
|
24 __date__ ="$11-Feb-2015 16:32:09$"
|
Daniel@0
|
25
|
Daniel@0
|
26 import sys
|
Daniel@0
|
27 import csv
|
Daniel@0
|
28 import json
|
Daniel@0
|
29
|
Daniel@0
|
30
|
Daniel@0
|
31 # global data format version
|
Daniel@0
|
32 dv=str(1)
|
Daniel@0
|
33
|
Daniel@0
|
34
|
Daniel@0
|
35 def getIdentifiers():
|
Daniel@0
|
36 # collect collection and perspective id
|
Daniel@0
|
37 cid = raw_input("Please Input Collection Identifier:")
|
Daniel@0
|
38 pid = raw_input("Please Input Perspective / Analyisis Identifier:")
|
Daniel@0
|
39
|
Daniel@0
|
40 # additional parameters
|
Daniel@0
|
41 params = raw_input("Input further parameters as string 'parameter1=value1_parameter2=value2...'")
|
Daniel@0
|
42 return (cid,pid,params)
|
Daniel@0
|
43
|
Daniel@0
|
44 def makeUrl(cid,pid,params):
|
Daniel@0
|
45 #"getCollectionPerspective_pid=chordSequences_cid=jazz_limit=100.json"
|
Daniel@0
|
46 s = "_"
|
Daniel@0
|
47 if not params == '':
|
Daniel@0
|
48 url = s.join(['getCollectionPerspective','pid='+ pid,'cid=' + cid,params,"dv="+dv])
|
Daniel@0
|
49 else:
|
Daniel@0
|
50 url = s.join(['getCollectionPerspective','pid='+ pid,'cid=' + cid,'dv='+dv])
|
Daniel@0
|
51
|
Daniel@0
|
52 return url
|
Daniel@0
|
53
|
Daniel@0
|
54
|
Daniel@0
|
55 # reads in any csv and returns a list of structure
|
Daniel@0
|
56 # time(float), data1, data2 ....data2
|
Daniel@0
|
57 def read_csv(filein = ''):
|
Daniel@0
|
58 output = []
|
Daniel@0
|
59 with open(filein, 'rb') as csvfile:
|
Daniel@0
|
60 contents = csv.reader(csvfile, delimiter=',', quotechar='"')
|
Daniel@0
|
61 for row in contents:
|
Daniel@0
|
62 output.append(row)
|
Daniel@0
|
63 return output
|
Daniel@0
|
64
|
Daniel@0
|
65 # write data into object formatted and create json output
|
Daniel@0
|
66 def data2apijson(data,cid,pid,params):
|
Daniel@0
|
67 url = makeUrl(cid,pid,params)
|
Daniel@0
|
68 if not type(data)=='dict':
|
Daniel@0
|
69 obj = {"query":url, "result": {pid:data}}
|
Daniel@0
|
70 else:
|
Daniel@0
|
71 obj = {"query":url, "result": data}
|
Daniel@0
|
72 return (json.dumps(obj), url)
|
Daniel@0
|
73
|
Daniel@0
|
74 # writes json to file
|
Daniel@0
|
75 def writejsonfile(jstring,url):
|
Daniel@0
|
76 f = open(url + ".json", "w");
|
Daniel@0
|
77 f.write(jstring)
|
Daniel@0
|
78 f.close
|
Daniel@0
|
79
|
Daniel@0
|
80 def data2json(data):
|
Daniel@0
|
81 # get further needed data such as url
|
Daniel@0
|
82 (cid,pid,params) = getIdentifiers()
|
Daniel@0
|
83 (jsondat,url) = data2apijson(data,cid,pid,params)
|
Daniel@0
|
84
|
Daniel@0
|
85 # write to file
|
Daniel@0
|
86 writejsonfile(jsondat,url)
|
Daniel@0
|
87
|
Daniel@0
|
88 #call this with csv input
|
Daniel@0
|
89 if __name__ == "__main__":
|
Daniel@0
|
90 # load data in file
|
Daniel@0
|
91 try:
|
Daniel@0
|
92 if len(sys.argv) < 2:
|
Daniel@0
|
93 csvfile = 'test.csv'
|
Daniel@0
|
94 else:
|
Daniel@0
|
95 csvfile = sys.argv[1]
|
Daniel@0
|
96 data = read_csv(csvfile)
|
Daniel@0
|
97 except:
|
Daniel@0
|
98 print "Please provide input csv file as command-line argument"
|
Daniel@0
|
99 quit()
|
Daniel@0
|
100
|
Daniel@0
|
101 data2json(data)
|