My favorites | Sign in
Project Logo
                
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/python
#
# Copyright (C) 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


__author__ = 'simonhildebrandt@gmail.com (Simon Hildebrandt)'

from lxml.html import parse
import urllib2
import gdata.calendar.service
import gdata.service
import atom.service
import gdata.calendar
import atom
from optparse import OptionParser
import sys
import string
import time
import os
from datetime import datetime

calendar_name = 'Solar calendar'
calendar_id = 'rgdehtq849lcsv24i9pbq89c38@group.calendar.google.com'
cal_uri = '/calendar/feeds/%s/private/full' % calendar_id
data_url = 'http://en.wikipedia.org/wiki/Solstice'
quadrants = [
(3, 'Equinox', 'northern spring/southern autumn'),
(6, 'Solstice', 'northern summer/southern winter'),
(9, 'Equinox', 'northern autumn/southern spring'),
(12, 'Solstice', 'northern winter/southern summer'),
]
layout = """%(type)s, %(desc)s.

%(cal_name)s generated from data on the Solstice Wikipedia page:
%(data_url)s
Author: Simon Hildebrandt
http://www.simonhildebrandt.com
Created using this script:
http://code.google.com/p/scraps/source/browse/trunk/scripts/solar.py
"""


class SolarCalendar:

def __init__(self, email, password):
"""Creates a CalendarService and provides ClientLogin auth details to it.
The email and password are required arguments for ClientLogin. The
CalendarService automatically sets the service to be 'cl', as is
appropriate for calendar. The 'source' defined below is an arbitrary
string, but should be used to reference your name or the name of your
organization, the app name and version, with '-' between each of the three
values. The account_type is specified to authenticate either
Google Accounts or Google Apps accounts. See gdata.service or
http://code.google.com/apis/accounts/AuthForInstalledApps.html for more
info on ClientLogin. NOTE: ClientLogin should only be used for installed
applications and not for multi-user web applications."""

self.cal_client = gdata.calendar.service.CalendarService()
self.cal_client.email = email
self.cal_client.password = password
self.cal_client.source = 'Solar-Calendar-1.0'
self.cal_client.ProgrammaticLogin()

def upload(self):
"""
1. Download the Wikipedia page about the Solistices and Equinoxes
2. Parse the page for the next few years of data
3. Get the appropriate calendar from Google calendar
4. Create events in the calendar matching the data
"""
doc = parse(data_url).getroot()
# Find the data table
table = doc.cssselect('table.wikitable')[0]
# Iterate over all the rows in the table
for row in table.cssselect('tr')[3:]:
# The first cell (and the only 'th' tag) is the year
year = int(row.cssselect('th')[0].text_content())
# The rest of the cells are pairs of day-of-the-month and time-of-the-day
data = row.cssselect('td')
# Step through collecting the days and turning them into integers
days = [int(x.text_content()) for x in data[0::2]]
# Step through collecting the times (24 hour)
times = [x.text_content() for x in data[1::2]]
# Combine our 'quadrant' data and the dates
for q, day, t in zip(quadrants, days, times):
month, etype, desc = q
hour, minute = [int(x) for x in t.split(':')]
date = datetime(year, month, day, hour, minute)
start_time = end_time = date.strftime('%Y-%m-%dT%H:%M:%S.000Z')
content = layout % dict(type=etype,desc=desc,cal_name=calendar_name,data_url=data_url)
event = gdata.calendar.CalendarEventEntry()
event.title = atom.Title(text=etype)
event.content = atom.Content(text=content)
print date, ' - ', etype
#event.where.append(gdata.calendar.Where(value_string=where))
event.when.append(gdata.calendar.When(start_time=start_time, end_time=end_time))
new_event = self.cal_client.InsertEvent(event, cal_uri)

def clean(self):
feed = self.cal_client.GetCalendarEventFeed(uri=cal_uri)
for event in feed.entry:
print 'deleting', event.title.text
self.cal_client.DeleteEvent(event.GetEditLink().href)


def main():
"""Runs the SolarCalendar application with the provided username and
and password values. Authentication credentials are required."""

# parse command line options
parser = OptionParser(
usage = "usage: %prog [options]",
version="%prog 1.0"
)
parser.add_option("-u", "--user", type="string", help="Google account username")
parser.add_option("-p", "--pwd", type="string", help="Google account password")
(options, args) = parser.parse_args(sys.argv)

if options.user and options.pwd:
calendar = SolarCalendar(options.user, options.pwd)
calendar.upload()
#calendar.clean()
else:
print 'Username and password required'

if __name__ == '__main__':
main()
Show details Hide details

Change log

r25 by simonhildebrandt on Dec 20, 2008   Diff
Small tweaks
Go to: 
Project members, sign in to write a code review

Older revisions

r24 by simonhildebrandt on Dec 20, 2008   Diff
Added 'solar' script, old updates to
website and the newest version of
Faultline
All revisions of this file

File info

Size: 5817 bytes, 139 lines
Hosted by Google Code