-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspreadsheetdata.py
executable file
·60 lines (51 loc) · 1.46 KB
/
spreadsheetdata.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import csv
import json
import datetime
import urllib
import urllib2
import smtplib
from xml.dom import minidom
try:
key = str(sys.argv[1])
except:
print "Missing Sheets Key"
sys.exit()
f_token = open("/home/csam/artist_scmp_com.google.spreadsheets.token", "r")
token_txt = f_token.read()
token = json.loads(token_txt)
f_token.close()
headers = { "Authorization": token["token_type"] + " " + token["access_token"] }
url = "https://spreadsheets.google.com/feeds/worksheets/%(key)s/private/basic" % { "key": key }
request = urllib2.Request(url, headers=headers)
response = urllib2.urlopen(request)
spreadsheet = response.read()
#print spreadsheet
cols = ["id", "title", "updated"]
try:
dom = minidom.parseString(spreadsheet)
except Exception as e:
print e
sys.exit()
entries = list()
for entry in dom.getElementsByTagName('entry'):
row = dict()
for cell in entry.childNodes:
if cell.tagName in cols:
try:
row[cell.tagName] = cell.firstChild.nodeValue.encode("utf8")
except:
row[cell.tagName] = cell.firstChild.nodeValue
if cell.tagName == "id":
row[cell.tagName] = row[cell.tagName].split("/")[len(row[cell.tagName].split("/"))-1]
entries.append(row)
cw = csv.DictWriter(sys.stdout, cols)
cw.writeheader()
for x in entries:
try:
cw.writerow(x)
except:
continue
print(x,sys.stderr)