Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 4th, 2012  |  syntax: None  |  size: 1.14 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import argparse
  2. import sys
  3.  
  4. import pymongo
  5. import tablib
  6.  
  7. def make_tablib_data(item):
  8.     rv = (
  9.         item.get('url', ''),
  10.         item.get('title', ''),
  11.         item.get('description', ''),
  12.         item.get('keywords', ''),
  13.         item.get('h1', ''),
  14.         item.get('h2', ''),
  15.         item.get('canonical', ''),
  16.     )
  17.     return rv
  18.  
  19. def main():
  20.     parser = argparse.ArgumentParser()
  21.     parser.add_argument('-c', '--client', dest='client', default=None)
  22.     parser.add_argument('-d', '--db', dest='db', default='urls')
  23.     parser.add_argument('-f', '--file', dest='output', default='output.csv')
  24.     args = parser.parse_args()
  25.     if args.client is None:
  26.         print('Please specify a client')
  27.         sys.exit(1)
  28.    
  29.     conn = pymongo.Connection()
  30.     db = conn[args.client]
  31.     col = db[args.db]
  32.    
  33.     headers = ('url', 'title', 'description', 'h1', 'h2', 'canonical',)
  34.     dataset = tablib.Dataset()
  35.    
  36.     urls = col.find()
  37.     for record in urls:
  38.         record_ = make_tablib_data(record)
  39.         dataset.append(record_)
  40.     conn.close()
  41.    
  42.     with open(args.output, 'wb') as f:
  43.         f.write(dataset.csv)
  44.  
  45. if __name__ == '__main__':
  46.     main()