Tuesday, July 10, 2007

My first Python program excites me: Source code

As someone has requested for the source code, here it is. Improvements are welcome.


#!/usr/bin/env python

from urllib import urlopen

try:
from xml.etree import ElementTree
except ImportError:

from elementtree import ElementTree
import gdata.spreadsheet.service
import gdata.service

import atom.service
import gdata.spreadsheet
import atom
import getopt
import sys

import string
from HTMLParser import HTMLParser
import re
from datetime import datetime


class bdoHTMLParser(HTMLParser):

def reset(self):

HTMLParser.reset(self)
self.getDate = None

self.rawdate = None
self.date = None

self.key = ''
self.data = {}



def handle_data(self, data):
if re.search(r'Unit Investment Trust Funds',data):

self.getDate = data
elif self.getDate:

try:
self.rawdate = data.split(' ', 1)[1].strip()

self.getDate = None
except:
print "Parsing date failed: %s" %(data)

elif re.search(r'BDO\D+Fund',data):
self.key = data

elif self.key:
self.data[self.key] = data.strip()

self.key = None

def main():
# SETUP

columns_header = ['phpmm', 'phpmmchange', 'mm', 'mmchange',

'phpbond', 'phpbondchange', 'bond', 'bondchange',
'balance', 'balancechange', 'equity', 'equitychange',

'fixedincome', 'fixedincomechange']
uitf_to_header_map = {'BDO Peso Money Market Fund':columns_header[0], 'BDO $ Money Market Fund':columns_header[2],

'BDO Peso Bond Fund':columns_header[4], 'BDO $ Bond Fund':columns_header[6],
'BDO Balanced Fund':columns_header[8], 'BDO Equity Fund':columns_header[10],

'BDO Fixed-Income Fund':columns_header[12]}

#ss_key = 'o04042323673667114780.4164488110474914499'
ss_key = 'pHJcUzPY3GTjD6jFA3zvLOQ'

ws_key = 'od6'

# GETTING THE SOURCE
page = urlopen('http://www.bdo.com.ph/NAV.asp')

html = page.read()
page.close()
print "Download Data Success"


# PARSE DATA
parse_data = bdoHTMLParser()
parse_data.feed(html)

parse_data.close()
print "Parsing Success"

# CONNECT TO GOOGLE SPREADSHEET
gd_client = gdata.spreadsheet.service.SpreadsheetsService()

gd_client.email = 'your_username'
gd_client.password = 'your_password'

gd_client.ProgrammaticLogin()
print "Login Success"

# CHECK DATE FORMAT
if re.match(r'\S{3,9}\s+\d{1,2},\s+\d{4}',parse_data.rawdate):

temp_date = datetime.strptime(string.join(parse_data.rawdate.split()), '%B %d, %Y')

parse_data.date = "%s/%s/%s" % (temp_date.month, temp_date.day, temp_date.year)

print 'Date Format Pass: %s' % (parse_data.date)

else:

print """Date Format Failed: %s\n
BDO Log Aborted.\n""" % (parse_data.rawdate)

return

# CHECK LAST DATE LOG
query = gdata.spreadsheet.service.ListQuery()

query.sq = 'date=%s' % (parse_data.date)

feed = gd_client.GetListFeed(ss_key, ws_key, query = query)

if len(feed.entry) != 0:
print """Entry Found.\nBDO Log Aborted.\n

%s %s""" % (feed.entry[0].title.text, feed.entry[0].content.text)

return

# CONSTRUCT DATA
data = {}
data['date'] = parse_data.date

for k, v in parse_data.data.items():

data[uitf_to_header_map[k]] = v
#print "%s = %s" % (k, v)

print 'Construct Data Success'


# WRITE DATA
entry = gd_client.InsertRow(data, ss_key, ws_key)

if isinstance(entry, gdata.spreadsheet.SpreadsheetsList):
print """BDO LOG Success\n

Enter: %s""" % (data)


if __name__ == '__main__':

main()





9 comments:

Jay Reeves said...

James,
Thanks for posting this so quickly.
Jay

Anonymous said...

by the amount of lines i would say your not new to programming. python is my first language so i hope i'll be good as you some day :). anyway thanx for the code it helps.

Anonymous said...

bought viagra fuerteventura viagra vs cialis free viagra without prescription cheapest viagra prices buy viagra in england viagra strips viagra dosages can women take viagra buy viagra australia herbal viagra viagra sample viagra free sites computer find viagra stories viagra substitute

Anonymous said...

Hello !.
You may , probably curious to know how one can make real money .
There is no need to invest much at first. You may begin to receive yields with as small sum of money as 20-100 dollars.

AimTrust is what you thought of all the time
The company incorporates an offshore structure with advanced asset management technologies in production and delivery of pipes for oil and gas.

Its head office is in Panama with offices everywhere: In USA, Canada, Cyprus.
Do you want to become really rich in short time?
That`s your chance That`s what you wish in the long run!

I`m happy and lucky, I began to get income with the help of this company,
and I invite you to do the same. If it gets down to choose a proper partner who uses your money in a right way - that`s AimTrust!.
I make 2G daily, and my first deposit was 1 grand only!
It`s easy to start , just click this link http://nabahyfat.kogaryu.com/hefawaju.html
and lucky you`re! Let`s take this option together to become rich

Anonymous said...

Hello !.
You re, I guess , probably curious to know how one can reach 2000 per day of income .
There is no initial capital needed You may start to receive yields with as small sum of money as 20-100 dollars.

AimTrust is what you need
AimTrust incorporates an offshore structure with advanced asset management technologies in production and delivery of pipes for oil and gas.

It is based in Panama with structures around the world.
Do you want to become an affluent person?
That`s your choice That`s what you really need!

I`m happy and lucky, I started to get real money with the help of this company,
and I invite you to do the same. If it gets down to choose a correct companion utilizes your savings in a right way - that`s it!.
I take now up to 2G every day, and my first deposit was 1 grand only!
It`s easy to get involved , just click this link http://butunapoh.freecities.com/aqujys.html
and lucky you`re! Let`s take this option together to become rich

Anonymous said...

Do you have copy writer for so good articles? If so please give me contacts, because this really rocks! :)

Anonymous said...

Your blog keeps getting better and better! Your older articles are not as good as newer ones you have a lot more creativity and originality now. Keep it up!
And according to this article, I totally agree with your opinion, but only this time! :)

Anonymous said...

Genial fill someone in on and this enter helped me alot in my college assignement. Thanks you for your information.

Anonymous said...

uovt nxiir porn hub wuhuin o uo k guc