imcrazytwkr

Rutracker CSV Postgres import script

Dec 11th, 2015
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.46 KB | None | 0 0
  1. import os
  2. import csv
  3. import pg8000
  4. connection = pg8000.connect(user='rutracker', password='dbpassword', database='rutracker')
  5.  
  6. # Configuration
  7. categories = [2, 8, 9, 18, 22, 23, 35]
  8.  
  9. # Creating table if not exists
  10. cursor = connection.cursor()
  11. cursor.execute("""CREATE TABLE IF NOT EXISTS shares(
  12.     id serial PRIMARY KEY,
  13.     category smallint NOT NULL DEFAULT 0,
  14.     subcategory smallint NOT NULL DEFAULT 0,
  15.     share_id integer NOT NULL,
  16.     share_hash char(40) NOT NULL,
  17.     share_name varchar(250) NOT NULL,
  18.     share_size bigint,
  19.     CONSTRAINT id_unique unique (share_id),
  20.     CONSTRAINT hash_unique unique (share_hash)
  21. );""")
  22. connection.commit()
  23. print('[OK] Database connection established')
  24.  
  25. dirs = list(filter(os.path.isdir, os.listdir(os.getcwd())))
  26. for dirt in dirs:
  27.     for category in categories:
  28.         if(os.path.isfile(dirt+'/category_'+str(category)+'.csv')):
  29.             file = open(dirt+'/category_'+str(category)+'.csv', 'r', encoding='UTF-8')
  30.             shares = list(csv.DictReader(file, fieldnames=('cat_id', 'cat_name', 'id', 'hash', 'name', 'size'), delimiter=';', quotechar='"'))
  31.             file.close()
  32.  
  33.             inserts = 0
  34.             updates = 0
  35.  
  36.             for share in shares:
  37.                 cursor.execute("SELECT EXISTS(SELECT id FROM shares WHERE share_hash=%s LIMIT 1);", [share['hash']])
  38.                 if cursor.fetchone()[0]:
  39.                     cursor.execute("UPDATE shares SET share_id=%s, share_name=%s, share_size=%s WHERE share_hash=%s;", [
  40.                         share['id'],
  41.                         share['name'],
  42.                         share['size'],
  43.                         share['hash']
  44.                     ])
  45.                     updates += 1
  46.                 else:
  47.                     cursor.execute("SELECT EXISTS(SELECT id FROM shares WHERE share_id=%s LIMIT 1);", [share['id']])
  48.                     if cursor.fetchone()[0]:
  49.                         cursor.execute("UPDATE shares SET share_hash=%s, share_name=%s, share_size=%s WHERE share_id=%s;", [
  50.                             share['hash'],
  51.                             share['name'],
  52.                             share['size'],
  53.                             share['id']
  54.                         ])
  55.                         updates += 1
  56.                     else:
  57.                         cursor.execute("INSERT INTO shares (category, subcategory, share_id, share_hash, share_name, share_size) VALUES (%s, %s, %s, %s, %s, %s)", [
  58.                             category,
  59.                             share['cat_id'],
  60.                             share['id'],
  61.                             share['hash'],
  62.                             share['name'],
  63.                             share['size']
  64.                         ])
  65.                         inserts += 1
  66.  
  67.             connection.commit()
  68.             if(updates==0):
  69.                 print("[OK] Pushing %s inserts for category #%s from dump at date %s" % (inserts, category, dirt))
  70.             else:
  71.                 print("[OK] Pushing %s inserts and %s updates for category #%s from dump at date %s" % (inserts, updates, category, dirt))
  72.  
  73. cursor.close()
Advertisement
Add Comment
Please, Sign In to add comment