Advertisement
jessicatemporal

Untitled

Sep 22nd, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.79 KB | None | 0 0
  1. import sys
  2. from logging import INFO, basicConfig, error, info
  3. from os import environ
  4. from time import sleep
  5.  
  6. import psycopg2
  7. import simplejson as json
  8. from tqdm import tqdm
  9.  
  10. basicConfig(format='%(levelname)s %(message)s', level=INFO)
  11. try:
  12.     table = sys.argv[1]
  13. except:
  14.     error('Usage: data_retrieval.py [table/view]')
  15.     sys.exit(1)
  16.  
  17. USER = environ.get('DB_USER')
  18. PASSWORD = environ.get('DB_PASSWORD')
  19. if not USER or not PASSWORD:
  20.     error('no USER or PASSWORD for connection found')
  21.     sys.exit(1)
  22.  
  23. info('Connecting to database...')
  24. conn = psycopg2.connect(database='twitter', user=USER, password=PASSWORD,
  25.                         host='143.107.137.90')
  26. cur = conn.cursor()
  27. sql = 'SELECT json FROM {};'.format(table)
  28. info('Executing SQL command...')
  29. cur.execute(sql)
  30. info('Fetching and writing data...')
  31.  
  32.  
  33. def fetch_and_save_data_to_file(db_cursor, filenames):
  34.     filenames['sampleresult'] = 'sample-' + filenames['result']
  35.     with open(filenames['result'], 'w') as result, \
  36.          open(filenames['sampleresult'], 'w') as sampleresult:
  37.         info('File: %s', filenames['result'])
  38.         i = 0
  39.         samples = []
  40.         for row in tqdm(db_cursor):
  41.             if i < filenames['opt']:
  42.                 i += 1
  43.                 samples.append(row[0])
  44.             result.write(json.dumps(row[0], result))
  45.             result.write('\n')
  46.         info('File: %s', filenames['sampleresult'])
  47.         for sample in tqdm(samples):
  48.             sampleresult.write(json.dumps(sample, sampleresult))
  49.             sampleresult.write('\n')
  50.     result.close()
  51.     sampleresult.close()
  52.     info('Closing conection...')
  53.     cur.close()
  54.     conn.close()
  55.  
  56. files = {
  57.             'result':'tweets-json',
  58.             'opt': 50,
  59.         }
  60. fetch_and_save_data_to_file(cur, files)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement