#! /usr/bin/python
# vim: fileencoding=utf-8 encoding=utf-8 et sw=4

# Copyright (C) 2009 Andrzej Zaborowski <balrogg@gmail.com>

import flickr
import flickrkey # Use your own
import json

import codecs, locale, sys
try:
    locale.setlocale(locale.LC_ALL, "en_US.UTF-8")
    encoding = locale.getlocale()[1]
    sys.stdout = codecs.getwriter(encoding)(sys.stdout, errors = "replace")
except locale.Error:
    pass

limit = 256
pages = 0
all_pages = 1
while pages < all_pages:
    pages += 1
    photos, all_pages = flickr.photos_search(per_page = limit, page = pages,
            machine_tags = 'osm:', media = 'photos',
            extras = 'owner_name,tags,url_m,date_taken')

    for photo in photos:
        flickrt = { 'id': photo.id, 'url': photo.raw_keys['url_m'] }
        for i in [ 'owner', 'ownername', 'datetaken', 'title' ]:
            if i in photo.raw_keys and photo.raw_keys[i]:
                flickrt[i] = photo.raw_keys[i]

        tags = []
        foursquare = ""
        dopplr = ""
        lastfm = ""
        upcoming = ""
        openplaques = ""
        openlibrary = ""
        for tag in photo.raw_keys['tags'].split(' '):
            if '=' not in tag: # non-machine tag
                tags.append(tag)
            elif tag[0:17] == 'foursquare:venue=':
                foursquare = tag[17:]
            elif tag[0:7] == 'dopplr:':
                dopplr = tag[7:]
            elif tag[0:13] == 'lastfm:event=':
                lastfm = tag[13:]
            elif tag[0:15] == 'upcoming:event=':
                upcoming = tag[15:]
            elif tag[0:15] == 'openplaques:id=':
                openplaques = tag[15:]
            elif tag[0:15] == 'openlibrary:id=':
                openlibrary = tag[15:]

        if tags:
            flickrt['tags'] = tags
        flickrt = json.dumps(flickrt).replace('\\', '\\\\').replace('"', '\\"')

        for tag in photo.raw_keys['tags'].split(' '):
            if tag[0:4] == 'osm:':
                if tag[4:9] == 'node=':
                    type = 'OSMTYPE_NODE'
                    id = tag[9:]
                elif tag[4:8] == 'way=':
                    type = 'OSMTYPE_WAY'
                    id = tag[8:]
                elif tag[4:13] == 'relation=':
                    type = 'OSMTYPE_RELATION'
                    id = tag[13:]
                else:
                    continue

                sys.stdout.write('{\n' +
                        '\t' + type + ',\n' +
                        '\t' + id + ',\n' +
                        '\t\"' + flickrt + '\",\n' +
                        '\t\"' + foursquare + '\",\n' +
                        '\t\"' + dopplr + '\",\n' +
                        '\t\"' + lastfm + '\",\n' +
                        '\t\"' + upcoming + '\",\n' +
                        '\t\"' + openplaques + '\",\n' +
                        '\t\"' + openlibrary + '\"\n' +
                        '},\n')

    sys.stderr.write(str(pages) + ' out of ' + str(all_pages) +
            ' pages of photos retrieved\n')
