cowsay-blog

commit 1743b491bea6b26252966129d93d35921cc0cacd

tree

parent:
518147f0b678135ee6d04a6c361898e61eb66841

Nick Mykins <nick.mykins@gmail.com>

2015-01-19T16:43:30-05:00

track cowpost.py

diff --git a/cowpost.py b/cowpost.py
new file mode 100644
index 0000000000000000000000000000000000000000..d11b6f43370f07a6d5ca095410551f8336e5c3d8
--- /dev/null
+++ b/cowpost.py
@@ -0,0 +1,56 @@
+import sqlite3
+import arrow
+from sh import cowsay
+import sys
+import os
+PATH = os.getcwd()
+
+def db_connect():
+    conn = sqlite3.connect('cowposts.db')
+    conn.row_factory = sqlite3.Row
+    return conn
+
+
+def db_init():
+    with db_connect() as conn:
+        cur = conn.cursor()
+        cur.execute('''
+            create table if not exists
+            cowposts (
+            id integer primary key autoincrement,
+            title text not null,
+            date text not null,
+            text text not null,
+            cowtext text not null)
+        ''')
+        conn.commit()
+        
+
+def db_insert(title, text):
+    values = dict(
+        date=arrow.now().format('YYYY-MM-DD HH:mm:ss'),
+        title=title,
+        text=text,
+        cowtext=str(cowsay(text)))
+    with db_connect() as conn:
+        cur = conn.cursor()
+        cur.execute('''
+            insert into cowposts
+            (date, title, text, cowtext)
+            values
+            (:date, :title, :text, :cowtext)
+        ''', values)
+        conn.commit()
+
+
+def main():
+	if not os.path.isfile(PATH + 'cowposts.db'):
+		db_init()
+	filename = sys.argv[1]
+	with open(filename, 'r') as f:
+		text = f.read()
+	db_insert(filename, text)
+
+
+if __name__ == '__main__':
+	main()
\ No newline at end of file