Skip to content

Instantly share code, notes, and snippets.

@y0n3l
Last active December 14, 2015 18:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save y0n3l/5129229 to your computer and use it in GitHub Desktop.
Save y0n3l/5129229 to your computer and use it in GitHub Desktop.
Create an iCal from items you should return to the Valbonne Mediathèque
#!/usr/bin/python
# -*- coding: utf-8 -*-
'''
Created on Mar 9, 2013
@author: y0n3l
'''
from datetime import datetime, date
from ftplib import FTP
from icalendar import Calendar, Event
from lxml.html.soupparser import fromstring
import mechanize
def getBooksReturnDatesFor(username, password, account):
booksForReturn = []
browser = mechanize.Browser()
browser.open("http://www.mediatheque-casa.fr/mediatheque-casa.fr/")
browser.select_form(name="form")
browser["username"] = username
browser["password"] = password
browser.submit()
response = browser.open("http://www.mediatheque-casa.fr/mediatheque-casa.fr/abonne/prets")
root = fromstring(response)
# get all the books
books = root.xpath("//div[@id='colContenuInner']//table//tr/td[position()=2]/a/text()")
# then get all their info (if available)
index=1
for book in books:
author = root.xpath("string(//div[@id='colContenuInner']//table//tr[position()=%i]/td[position()=3])" % (index+1))
returnDate = root.xpath("string(//div[@id='colContenuInner']//table//tr[position()=%i]/td[@class='date_retour']/text())" % (index+1))
date = datetime.strptime(returnDate.strip(),'%d/%m/%Y')
index=index+1
booksForReturn.append({"book":book,"author":author,"returnDate":date, 'account':account})
return booksForReturn
def getICalForBooksReturnDates(returnDates):
cal = Calendar()
cal.add('version', '2.0')
cal.add('X-WR-CALNAME', u'Mediathèque Valbonne')
cal.add('X-WR-TIMEZONE', 'Europe/Paris')
for item in returnDates:
book = item['book']
author = item['author']
date = item['returnDate']
account = item['account']
event = Event()
event.add('summary', "RENDRE: %s" % book)
event.add('description', "Auteur: %s\nSur la carte de %s" % (author, account))
event.add('location', u'Médiathèque, Valbonne, France')
event['DTSTART']= date.strftime('%Y%m%d')
event['DTEND']= date.strftime('%Y%m%d')
event.add('class', 'PUBLIC')
cal.add_component(event)
return cal.to_ical()
if __name__=="__main__":
print getBooksReturnDatesFor('X0000xxxxxx', 'password', 'Account for Y0n3l')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment