<s> import csv <EOL> from couchdbkit import Consumer <EOL> def download_changes ( db , limit , target ) : <EOL> """<STR_LIT>""" <EOL> writer = csv . writer ( target , dialect = csv . excel ) <EOL> keys = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:date>' , <EOL> '<STR_LIT:id>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> writer . writerow ( keys ) <EOL> for row in get_recent_changes ( db , limit ) : <EOL> writer . writerow ( [ row [ k ] for k in keys ] ) <EOL> def get_recent_changes ( db , limit ) : <EOL> c = Consumer ( db ) <EOL> changes = c . fetch ( limit = limit , descending = True , include_docs = True ) [ '<STR_LIT>' ] <EOL> for row in changes : <EOL> yield { <EOL> '<STR_LIT:id>' : row [ '<STR_LIT:id>' ] , <EOL> '<STR_LIT>' : row [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : row [ '<STR_LIT>' ] . get ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : row [ '<STR_LIT>' ] . get ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT:date>' : _guess_date ( row [ '<STR_LIT>' ] ) , <EOL> } <EOL> def _guess_date ( doc ) : <EOL> properties_to_check = ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:date>' , <EOL> ) <EOL> for p in properties_to_check : <EOL> val = doc . get ( p ) <EOL> if val : <EOL> return val <EOL> return '<STR_LIT>' </s>
