#! /usr/bin/python -u import flickr from rdflib import Graph from rdflib import URIRef, Literal, BNode from rdflib import RDF, RDFS from namespaces import FLICKR, FOAF, DC, DCTERMS import time flickr.API_KEY = file('.flickr').read().strip() def photo2rdf(photo_id): store = Graph() store.bind("flickr", FLICKR) store.bind("foaf", FOAF) store.bind("dc", DC) store.bind("dcterms", DCTERMS) photo = flickr.Photo(photo_id) uri = URIRef('http://photos%s.flickr.com/%s_%s_o.jpg' % (photo.server, photo.id, photo.secret)) store.add((uri, RDF.type, FOAF['Image'])) store.add((uri, DC['title'], Literal(photo.title))) if photo.description: store.add((uri, DC['description'], Literal(photo.description))) thumb = URIRef('http://photos%s.flickr.com/%s_%s_t.jpg' % (photo.server, photo.id, photo.secret)) store.add((thumb, RDF.type, FOAF['Image'])) store.add((uri, FOAF['thumbnail'], thumb)) try: store.add((uri, DC['date'], Literal(time.strftime('%Y-%M-%dT%H:%I:%S', time.gmtime(float(photo.dateposted)))))) except: store.add((uri, DC['date'], Literal(photo.dateposted))) try: store.add((uri, DCTERMS['issued'], Literal(photo.datetaken.replace(' ', 'T')))) except: store.add((uri, DCTERMS['issued'], Literal(photo.datetaken))) # Comments with Annotea, some other day # Tags for tag in photo.tags: taguri = URIRef('http://flickr.com/photos/%s/tags/%s/' % (tag.author.id, tag.text)) # Tag data store.add((taguri, RDF.type, FLICKR.Tag)) store.add((taguri, RDFS.label, Literal(tag.text))) store.add((taguri, RDFS.comment, Literal(tag.raw))) store.add((uri, FLICKR.tagged, taguri)) store.add((uri, DC['keyword'], Literal(tag.text))) store.save('%s.rdf' % photo_id, format='pretty-xml', encoding='iso-8859-1') if __name__ == '__main__': import sys if len(sys.argv) > 1: for photo_id in sys.argv[1:]: photo2rdf(photo_id) else: photo2rdf('31211660')