public
Created

Download Almoco do Spreadsheet

  • Download Gist
DownloadAlmoço.py
Python
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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
# -*- coding: utf-8 -*-
#!/usr/bin/python
 
import re, urllib, urllib2, json
 
class Spreadsheet(object):
def __init__(self, key):
super(Spreadsheet, self).__init__()
self.key = key
 
class Client(object):
def __init__(self, email, password):
super(Client, self).__init__()
self.email = email
self.password = password
def _get_auth_token(self, email, password, source, service):
url = "https://www.google.com/accounts/ClientLogin"
params = {
"Email": email, "Passwd": password,
"service": service,
"accountType": "HOSTED_OR_GOOGLE",
"source": source
}
req = urllib2.Request(url, urllib.urlencode(params))
return re.findall(r"Auth=(.*)", urllib2.urlopen(req).read())[0]
def get_auth_token(self):
source = type(self).__name__
return self._get_auth_token(self.email, self.password, source, service="wise")
def download(self, spreadsheet, gid=0, format="csv"):
url_format = "https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key=%s&exportFormat=%s&gid=%i"
headers = {
"Authorization": "GoogleLogin auth=" + self.get_auth_token(),
"GData-Version": "3.0"
}
req = urllib2.Request(url_format % (spreadsheet.key, format, gid), headers=headers)
return urllib2.urlopen(req)
 
import datetime
def today():
"""
return today's date in google spreadsheet format
"""
return str(datetime.date.today().month)+"/"+str(datetime.date.today().day)+"/"+str(datetime.date.today().year)
 
def get_list_for(_date, _list):
r = []
for order in _list:
# No time to better code :(
if _date==order['Timestamp'].split(" ")[0]:
r.append(order)
return r;
 
def generate_email_body(_list):
body=""
order_number=1
for order in _list:
body+= "------ Pedido %s ------"%(str(order_number))+'\n'
order_number+=1
for k in order.keys():
if order[k] != "" and k != "Timestamp":
if k=="Username":
body+= "Nome: "+ order[k][0:order[k].index('@')]+'\n'
else:
body+= k + " : "+ order[k]+'\n'
body+="\n"
return body
 
 
if __name__ == "__main__":
import getpass
import csv
email = "" # (your email here)
password = ""
spreadsheet_id = "" # (spreadsheet id here)
 
# Create client and spreadsheet objects
gs = Client(email, password)
ss = Spreadsheet(spreadsheet_id)
# Request a file-like object containing the spreadsheet's contents
csv_file = gs.download(ss)
# Parse as CSV and print the rows
#for row in csv.reader(csv_file):
# out= ", ".join(row)
reader = csv.DictReader(csv_file)
 
day_list = get_list_for(today(), [ row for row in reader ]);
print generate_email_body(day_list)
#out = json.dumps(day_list, ensure_ascii=False)
#print out

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.