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
McSelbsttriggertriggeriaC
McUngläubiaC
McAbfuckiaC
Mc200KGBeugiaC
McParanoiaC
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):
# Load dotenv from file
load_dotenv('.env')

View File

@ -1,26 +1,166 @@
# -*- coding: utf-8 -*-
import sqlite3
import sys
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):
with open(os.path.join(MIGRATE, file), 'r') as fp:
return [(l.replace('\n', ''),) for l in fp]
def migrate_mcmaniacs():
with open('migrate/mcmaniacs.txt', 'r') as 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):
db.executemany('insert into {table} ({column}) values (?)'.
format(table=table, column=column), data)
db.commit()
def migrate_kills():
with open('migrate/kills.txt', 'r') as fp:
for m in fp:
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__':
db = sqlite3.connect('db.sqlite' if len(sys.argv) is 0 else sys.argv[0])
batch_insert('mcmaniacs', 'item', read_file('mcmaniac.txt'))
batch_insert('yiffs', 'item', read_file('yiff.txt'))
def migrate_yiffs():
with open('migrate/yiffs.txt', 'r') as fp:
for m in fp:
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',
'nxy.plugins.storage']
def add_quote(self, mask, nick, quote):
def add_quote(self, mask, nick, quote, channel):
# Parse nick from "<@foobar>" like strings
nick = NICK_REGEX.match(nick).group(1)
@ -47,10 +47,10 @@ class Quotes(DatabasePlugin):
# Insert quote into database
self.cur.execute('''
insert into
quotes (nick, item)
quotes (nick, item, channel, created_by)
values
(%s, %s)
''', [nick, quote])
''', [nick, quote, channel, mask.nick])
def delete_quote(self, nick, quote):
index, order, _ = parse_int(quote, select=False)
@ -96,7 +96,7 @@ class Quotes(DatabasePlugin):
try:
# Anybody can add
if cmd == 'add':
self.add_quote(mask, nick, quote)
self.add_quote(mask, nick, quote, target)
# But only admins can delete
elif cmd == 'del' and self.guard.has_permission(mask, 'admin'):
self.delete_quote(nick, quote)
@ -138,7 +138,7 @@ class Quotes(DatabasePlugin):
from
ranked_quotes
where
nick like %s
lower(nick) like lower(%s)
order by
{order}
limit

View File

@ -26,22 +26,22 @@ class Rape(DatabasePlugin):
# Fetch result from database
self.cur.execute('''
select
amount
fines
from
owes
users
where
nick = %s
lower(nick) = lower(%s)
''', [nick])
owes = self.cur.fetchone()
# Colorize owe amount and return string
if owes:
amount = '4${total}'.format(total=owes['amount'])
fines = '4${fines}'.format(fines=owes['fines'])
else:
amount = '3$0'
fines = '3$0'
# 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
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
self.cur.execute('''
insert into
owes (nick, amount)
users (nick, host, fines)
values
(%s, %s)
(%s, %s, %s)
on conflict (nick) do update set
amount = owes.amount + excluded.amount
host = excluded.host,
fines = users.fines + excluded.fines
returning
amount
''', [nick, fine])
fines
''', [nick, mask.host, fine])
self.con.commit()
# Get reason based on rand value
@ -81,7 +82,7 @@ class Rape(DatabasePlugin):
.format(nick=nick,
fine=fine,
reason=reason,
total=self.cur.fetchone()['amount']))
total=self.cur.fetchone()['fines']))
except Error:
# Rollback transaction on error
self.con.rollback()

View File

@ -35,7 +35,7 @@ class Seen(DatabasePlugin):
from
seens
where
nick = %s
lower(nick) = lower(%s)
''', [nick])
seen = self.cur.fetchone()
@ -53,18 +53,21 @@ class Seen(DatabasePlugin):
@irc3.event(r'(?i)^:(?P<mask>\S+) PRIVMSG (?P<target>\S+) :(?P<msg>.*)')
def save(self, mask: str, target: str, msg: str):
mask = IrcString(mask)
try:
# Insert or update if user writes a message
self.cur.execute('''
insert into
seens (nick, channel, message, seen_at)
seens (nick, host, channel, message, seen_at)
values
(%s, %s, %s, %s)
(%s, %s, %s, %s, %s)
on conflict (nick) do update set
host = excluded.host,
channel = excluded.channel,
seen_at = excluded.seen_at,
message = excluded.message
''', [IrcString(mask).nick, target, msg, datetime.now()])
''', [mask.nick, mask.host, target, msg, datetime.now()])
self.con.commit()
except 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.utils import IrcString
from . import Plugin
from . import DatabasePlugin
RAINBOW = (5, 3, 7, 8, 9, 3, 10, 12, 2, 6, 13)
RAINBOW_LEN = len(RAINBOW)
@irc3.plugin
class Useless(Plugin):
requires = ['irc3.plugins.command']
class Useless(DatabasePlugin):
requires = ['irc3.plugins.command',
'nxy.plugins.storage']
@irc3.event(r'(?i)^:\S+ PRIVMSG (?P<target>\S+) :(?P<msg>huehuehue)$')
def huehuehue(self, target: str, msg: str):
@ -37,6 +38,44 @@ class Useless(Plugin):
self.bot.privmsg(target, '\x02[{msg} INTENSIFIES]'.format(
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
def storyofpomfface(self, mask: IrcString, target: IrcString,
args: DocOptDict):

View File

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