Moderator: Moderators
#!/usr/bin/python
#
# pt_notes_import.py
#
# Script to import poker site notes into PT3 database
import psycopg2
import sys
import getopt
import codecs
import re
def Usage():
print("Usage:\n\t%s --database {dbname} --user {username} --password {password} [--site {stars|??}] --file {notes_file}" % sys.argv[0])
def Main(argv):
"""Main program execution"""
db, user, password, site, file = ReadArgs(argv)
conn = GetDbConnection(db, user, password)
notes = ReadNotes(site, file)
WriteNotes(conn, notes)
def ReadNotes(site, file):
"""Generator function which yields (user, note) tuples"""
if 'stars' == site:
# stars notes are in utf-16 so we have to decode the data
f = codecs.open(file, encoding='utf-16')
player = None
regex = re.compile(r"\[player=(.*)\]")
note = []
for line in f:
m = regex.match(line)
if m:
# We found a new player.
# If player was previously set, return their notes
if player:
yield player.encode('ascii', 'ignore'), ''.join(note).strip()
# reset note
note = []
# Remember the player
player = m.group(1)
# If this isn't a player line, then it's a note. Save it
elif player:
note.append(line.encode('ascii', 'ignore'))
#if 'fulltilt' == site:
# ...
else:
print "Error: Unsupported site format '%s'" % site
sys.exit()
def WriteNotes(conn, notes):
"""Given notes of the form (player, note),
appends them to the PT notes table using the connection conn"""
cur = conn.cursor();
insert = """INSERT INTO notes (id_note, id_x, enum_type, date_note, notes)
SELECT n, id_player, 'P', now(), %(note)s FROM player,
(SELECT COUNT(*)+1 AS n FROM notes) n
WHERE player_name = %(player)s"""
for player, note in notes:
params = {'player':player, 'note':note}
cur.execute(insert, params)
conn.commit()
cur.close()
def GetDbConnection(db, user, password):
"""Returns a db connection object"""
try:
connString = "dbname='%s' user='%s' password='%s'" % (db, user, password)
conn = psycopg2.connect(connString)
return conn
except Exception, e:
print "Error: Unable to connect to the database"
print e
sys.exit()
def ReadArgs(argv):
"""Parses command line arguments"""
shortArgDef = "d:u:p:s:f:"
longArgDef = ["database=", "user=", "password=", "site=", "file="]
db = None
user = None
password = None
site = 'stars'
file = None
try:
opts, args = getopt.getopt(argv, shortArgDef, longArgDef)
for o, a in opts:
if o in ('-d', '--database'):
db = a
if o in ('-u', '--user'):
user = a
if o in ('-p', '--password'):
password = a
if o in ('-s', '--site'):
site = a
if o in ('-f', '--file'):
file = a
if not db or not user or not password or not site or not file:
Usage()
sys.exit()
return db, user, password, site, file
except getopt.GetoptError:
Usage()
sys.exit()
# Call main
if __name__ == "__main__":
Main(sys.argv[1:])
Return to Custom Stats, Reports, and SQL [Read Only]
Users browsing this forum: No registered users and 6 guests