Added migrate script, ready to roll i guess

This commit is contained in:
mrhanky 2017-06-30 17:47:24 +02:00
parent a4c98ed538
commit e18bf0d23d
No known key found for this signature in database
GPG Key ID: 67D772C481CB41B8
11 changed files with 289 additions and 54 deletions

View File

@ -279,5 +279,37 @@ McÖdiaC
McSchwuliaC McSchwuliaC
McSelbsttriggertriggeriaC McSelbsttriggertriggeriaC
McUngläubiaC McUngläubiaC
McAbfuckiaC McParanoiaC
Mc200KGBeugiaC McWaliaC
McWaldiaC
McTreniaC
McBurkiaC
McSchnefalliaC
McAmmoniaC
McAmmoniaC
ichbingarkeinmaniac
McThcZäpfcheniaC
McImperativiaC
McGysiaC
McDrogiaC
McGefängnisiaC
McArschseksiaC
McKommentarboxeinfachdirektunterdemletztenkommentariaC
McHeisseOhreniaC
McManiulo
McRallyaC
McMöchtiaC
McOdroidiaC
McRektiaC
McMemeiaC
McHantelstangiaC
Mc1iaC
McNixfunktioniertiaC
McBigMac
McExtremstschwuliaC
McSchnelliaC
McRomantiaC
McOssiaC
McHaskelliaC
McSchlafiaC
McGenderiaC

BIN
migrate/old.db Normal file

Binary file not shown.

View File

@ -23,6 +23,7 @@ CFG_DEV = {
} }
# TODO: regex
def main(cfg_file): def main(cfg_file):
# Load dotenv from file # Load dotenv from file
load_dotenv('.env') load_dotenv('.env')

View File

@ -1,26 +1,166 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import sqlite3
import sys
import os import os
import sqlite3
from datetime import datetime
MIGRATE = 'migrate' import psycopg2
from dotenv import load_dotenv
from psycopg2 import Error
from psycopg2.extras import DictCursor
db = None load_dotenv(os.environ.get('DOTENV_FILE', '.env'))
con = psycopg2.connect(os.environ['DATABASE_URI'])
cur = con.cursor(cursor_factory=DictCursor)
old = sqlite3.connect(os.path.join('migrate', 'old.db'))
old.row_factory = sqlite3.Row
old_cur = old.cursor()
def read_file(file): def migrate_mcmaniacs():
with open(os.path.join(MIGRATE, file), 'r') as fp: with open('migrate/mcmaniacs.txt', 'r') as fp:
return [(l.replace('\n', ''),) for l in fp] for m in fp:
try:
cur.execute('''
insert into
mcmaniacs (item)
values
(%s)
''', [m.strip()])
con.commit()
except Error as ex:
print(ex)
con.rollback()
def batch_insert(table, column, data): def migrate_kills():
db.executemany('insert into {table} ({column}) values (?)'. with open('migrate/kills.txt', 'r') as fp:
format(table=table, column=column), data) for m in fp:
db.commit() try:
cur.execute('''
insert into
kills (item)
values
(%s)
''', [m.replace('{user}', '{nick}').strip()])
con.commit()
except Error as ex:
print(ex)
con.rollback()
if __name__ == '__main__': def migrate_yiffs():
db = sqlite3.connect('db.sqlite' if len(sys.argv) is 0 else sys.argv[0]) with open('migrate/yiffs.txt', 'r') as fp:
batch_insert('mcmaniacs', 'item', read_file('mcmaniac.txt')) for m in fp:
batch_insert('yiffs', 'item', read_file('yiff.txt')) try:
cur.execute('''
insert into
yiffs (item)
values
(%s)
''', [m.replace('{user}', '{nick}').strip()])
con.commit()
except Error as ex:
print(ex)
con.rollback()
def migrate_quotes():
old_cur.execute('''
select
*
from
quote
''')
for q in old_cur.fetchall():
try:
cur.execute('''
insert into
quotes (nick, item, channel, created_by, created_at)
values
(%s, %s, %s, %s, %s)
''', [q['nick'].strip(),
q['msg'].strip(),
q['chan'].strip(),
q['add_nick'].strip(),
datetime.fromtimestamp(q['time'])])
con.commit()
except Error as ex:
print(ex)
con.rollback()
def migrate_seens():
old_cur.execute('''
select
*
from
seen
''')
seens = {}
for s in old_cur.fetchall():
nick = s['name'].strip()
s = [nick,
s['host'].strip(),
s['chan'].strip(),
s['quote'].strip(),
s['time']]
if nick not in seens or s[4] > seens[nick][4]:
seens[nick] = s
for s in seens.values():
s[4] = datetime.fromtimestamp(s[4])
try:
cur.execute('''
insert into
seens (nick, host, channel, message, seen_at)
values
(%s, %s, %s, %s, %s)
''', s)
con.commit()
except Error as ex:
print(ex)
con.rollback()
def migrate_users():
old_cur.execute('''
select
*
from
users
''')
for u in old_cur.fetchall():
husbando = u['husbando'].strip() if u['husbando'] else None
waifu = u['waifu'].strip() if u['waifu'] else None
fines = u['fines'] or None
if husbando or waifu or fines:
try:
cur.execute('''
insert into
users (nick, host, husbando, waifu, fines)
values
(%s, %s, %s, %s, %s)
''', [u['nick'].strip(),
u['mask'].strip() if u['mask'] else None,
husbando,
waifu,
fines])
con.commit()
except Error as ex:
print(ex)
con.rollback()
migrate_kills()
migrate_yiffs()
migrate_mcmaniacs()
migrate_quotes()
migrate_seens()
migrate_users()

View File

@ -37,7 +37,7 @@ class Quotes(DatabasePlugin):
requires = ['irc3.plugins.command', requires = ['irc3.plugins.command',
'nxy.plugins.storage'] 'nxy.plugins.storage']
def add_quote(self, mask, nick, quote): def add_quote(self, mask, nick, quote, channel):
# Parse nick from "<@foobar>" like strings # Parse nick from "<@foobar>" like strings
nick = NICK_REGEX.match(nick).group(1) nick = NICK_REGEX.match(nick).group(1)
@ -47,10 +47,10 @@ class Quotes(DatabasePlugin):
# Insert quote into database # Insert quote into database
self.cur.execute(''' self.cur.execute('''
insert into insert into
quotes (nick, item) quotes (nick, item, channel, created_by)
values values
(%s, %s) (%s, %s)
''', [nick, quote]) ''', [nick, quote, channel, mask.nick])
def delete_quote(self, nick, quote): def delete_quote(self, nick, quote):
index, order, _ = parse_int(quote, select=False) index, order, _ = parse_int(quote, select=False)
@ -96,7 +96,7 @@ class Quotes(DatabasePlugin):
try: try:
# Anybody can add # Anybody can add
if cmd == 'add': if cmd == 'add':
self.add_quote(mask, nick, quote) self.add_quote(mask, nick, quote, target)
# But only admins can delete # But only admins can delete
elif cmd == 'del' and self.guard.has_permission(mask, 'admin'): elif cmd == 'del' and self.guard.has_permission(mask, 'admin'):
self.delete_quote(nick, quote) self.delete_quote(nick, quote)
@ -138,7 +138,7 @@ class Quotes(DatabasePlugin):
from from
ranked_quotes ranked_quotes
where where
nick like %s lower(nick) like lower(%s)
order by order by
{order} {order}
limit limit

View File

@ -26,22 +26,22 @@ class Rape(DatabasePlugin):
# Fetch result from database # Fetch result from database
self.cur.execute(''' self.cur.execute('''
select select
amount fines
from from
owes users
where where
nick = %s lower(nick) = lower(%s)
''', [nick]) ''', [nick])
owes = self.cur.fetchone() owes = self.cur.fetchone()
# Colorize owe amount and return string # Colorize owe amount and return string
if owes: if owes:
amount = '4${total}'.format(total=owes['amount']) fines = '4${fines}'.format(fines=owes['fines'])
else: else:
amount = '3$0' fines = '3$0'
# Return total owes # Return total owes
return '{nick} owes: \x03{amount}\x03'.format(nick=nick, amount=amount) return '{nick} owes: \x03{fines}\x03'.format(nick=nick, fines=fines)
@command @command
def rape(self, mask: IrcString, target: IrcString, args: DocOptDict): def rape(self, mask: IrcString, target: IrcString, args: DocOptDict):
@ -61,14 +61,15 @@ class Rape(DatabasePlugin):
# Insert or add fine to database and return total owe # Insert or add fine to database and return total owe
self.cur.execute(''' self.cur.execute('''
insert into insert into
owes (nick, amount) users (nick, host, fines)
values values
(%s, %s) (%s, %s, %s)
on conflict (nick) do update set on conflict (nick) do update set
amount = owes.amount + excluded.amount host = excluded.host,
fines = users.fines + excluded.fines
returning returning
amount fines
''', [nick, fine]) ''', [nick, mask.host, fine])
self.con.commit() self.con.commit()
# Get reason based on rand value # Get reason based on rand value
@ -81,7 +82,7 @@ class Rape(DatabasePlugin):
.format(nick=nick, .format(nick=nick,
fine=fine, fine=fine,
reason=reason, reason=reason,
total=self.cur.fetchone()['amount'])) total=self.cur.fetchone()['fines']))
except Error: except Error:
# Rollback transaction on error # Rollback transaction on error
self.con.rollback() self.con.rollback()

View File

@ -35,7 +35,7 @@ class Seen(DatabasePlugin):
from from
seens seens
where where
nick = %s lower(nick) = lower(%s)
''', [nick]) ''', [nick])
seen = self.cur.fetchone() seen = self.cur.fetchone()
@ -53,18 +53,21 @@ class Seen(DatabasePlugin):
@irc3.event(r'(?i)^:(?P<mask>\S+) PRIVMSG (?P<target>\S+) :(?P<msg>.*)') @irc3.event(r'(?i)^:(?P<mask>\S+) PRIVMSG (?P<target>\S+) :(?P<msg>.*)')
def save(self, mask: str, target: str, msg: str): def save(self, mask: str, target: str, msg: str):
mask = IrcString(mask)
try: try:
# Insert or update if user writes a message # Insert or update if user writes a message
self.cur.execute(''' self.cur.execute('''
insert into insert into
seens (nick, channel, message, seen_at) seens (nick, host, channel, message, seen_at)
values values
(%s, %s, %s, %s) (%s, %s, %s, %s, %s)
on conflict (nick) do update set on conflict (nick) do update set
host = excluded.host,
channel = excluded.channel, channel = excluded.channel,
seen_at = excluded.seen_at, seen_at = excluded.seen_at,
message = excluded.message message = excluded.message
''', [IrcString(mask).nick, target, msg, datetime.now()]) ''', [mask.nick, mask.host, target, msg, datetime.now()])
self.con.commit() self.con.commit()
except Error: except Error:
# Rollback transaction on error # Rollback transaction on error

View File

@ -6,15 +6,16 @@ from docopt import Dict as DocOptDict
from irc3.plugins.command import command from irc3.plugins.command import command
from irc3.utils import IrcString from irc3.utils import IrcString
from . import Plugin from . import DatabasePlugin
RAINBOW = (5, 3, 7, 8, 9, 3, 10, 12, 2, 6, 13) RAINBOW = (5, 3, 7, 8, 9, 3, 10, 12, 2, 6, 13)
RAINBOW_LEN = len(RAINBOW) RAINBOW_LEN = len(RAINBOW)
@irc3.plugin @irc3.plugin
class Useless(Plugin): class Useless(DatabasePlugin):
requires = ['irc3.plugins.command'] requires = ['irc3.plugins.command',
'nxy.plugins.storage']
@irc3.event(r'(?i)^:\S+ PRIVMSG (?P<target>\S+) :(?P<msg>huehuehue)$') @irc3.event(r'(?i)^:\S+ PRIVMSG (?P<target>\S+) :(?P<msg>huehuehue)$')
def huehuehue(self, target: str, msg: str): def huehuehue(self, target: str, msg: str):
@ -37,6 +38,44 @@ class Useless(Plugin):
self.bot.privmsg(target, '\x02[{msg} INTENSIFIES]'.format( self.bot.privmsg(target, '\x02[{msg} INTENSIFIES]'.format(
msg=msg.upper())) msg=msg.upper()))
@command
def kill(self, mask:IrcString,target:IrcString,args:DocOptDict):
"""Kills a user with a random message.
%%kill [<nick>]
"""
nick = args.get('<nick>') or mask.nick
self.cur.execute('''
select
item
from
kills
order by
random()
limit
1
''')
return self.cur.fetchone()['item'].format(nick=nick)
@command
def yiff(self, mask:IrcString,target:IrcString,args:DocOptDict):
"""Yiffs a user with a random message.
%%yiff [<nick>]
"""
nick = args.get('<nick>') or mask.nick
self.cur.execute('''
select
item
from
yiffs
order by
random()
limit
1
''')
return self.cur.fetchone()['item'].format(nick=nick)
@command @command
def storyofpomfface(self, mask: IrcString, target: IrcString, def storyofpomfface(self, mask: IrcString, target: IrcString,
args: DocOptDict): args: DocOptDict):

View File

@ -1,7 +1,10 @@
create table if not exists quotes ( create table if not exists quotes (
id serial primary key, id serial primary key,
nick varchar(30) not null, nick varchar(30) not null,
item text not null, item text not null,
channel varchar(32) not null,
created_by varchar(30) not null,
created_at timestamp not null default current_timestamp,
unique (nick, item) unique (nick, item)
); );
@ -31,17 +34,33 @@ create table if not exists tells (
); );
create table if not exists seens ( create table if not exists seens (
id serial primary key, id serial primary key,
nick varchar(30) not null, nick varchar(30) not null,
channel varchar(32) not null, host varchar(255) not null,
message text not null, channel varchar(32) not null,
seen_at timestamp not null, message text not null,
seen_at timestamp not null default current_timestamp,
unique (nick) unique (nick)
); );
create table if not exists owes ( create table if not exists users (
id serial primary key, id serial primary key,
nick varchar(30) not null, nick varchar(30) not null,
amount integer not null, host varchar(255) not null,
husbando varchar(255) null,
waifu varchar(255) null,
fines integer default 0,
unique (nick) unique (nick)
); );
create table if not exists kills (
id serial primary key,
item text not null,
unique (item)
);
create table if not exists yiffs (
id serial primary key,
item text not null,
unique (item)
);