Advertisement
Tyler_Elric

py-mBlog.py

Jan 19th, 2012
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.95 KB | None | 0 0
  1. import re
  2. import sqlite3 as sqlite
  3.  
  4. major = 1
  5. minor = 0
  6. bugfix = 0
  7.  
  8. class Blog:
  9.     isUser = re.compile(r'usr:\s*(.+)',re.IGNORECASE)
  10.     isTitle = re.compile(r'title:\s*(.+)',re.IGNORECASE)
  11.     isTag = re.compile(r'tag:\s*(.+)',re.IGNORECASE)
  12.     isCat = re.compile(r'cat:\s*(.+)',re.IGNORECASE)
  13.     def __init__(self):
  14.         self._database = sqlite.connect('blog.db')
  15.         self._database.row_factory = sqlite.Row
  16.         self.cursor = self._database.cursor()
  17.         self.cursor.execute("Create Table if not exists Users(ID integer primary key default 0,Name text not null, email text not null, pw text not null level default 1")
  18.         self.cursor.execute("Create Table if not exists Posts(ID integer primary key default 0,Title text not null, Author integer not null, Content text not null, Categories integer not null,Tags text not null, Date default CURRENT_DATE)")
  19.     def Users(self,authors=True):
  20.         if authors: return self.cursor.execute("Select ID, Name, Email from Users where Level > 1")
  21.         return self.cursor.execute("Select ID, Name, Email from Users")
  22.     def Posts(self,context='news'):
  23.         ret = [] #Declare an initial array
  24.         if context.lower() == 'news': ret = self.posts[0:10]
  25.         else:
  26.             for case in context.split(r'/'):
  27.                 match = self.isUser.match(case)
  28.                 if match is not None:
  29.                     ret.extend(post for post in self.cursor.execute("Select Users.Name,Posts.Title,Posts.Content,Posts.Date,Post.Tags,Categories.Name from Users,Posts,Categories where Users.ID==Posts.Author and Users.Name like ? and Categories.ID==Posts.Category",(match.group(1),)) if post not in ret)
  30.                     continue
  31.                 match = self.isTitle.match(case)
  32.                 if match is not None:
  33.                     ret.extend(post for post in self.cursor.execute("Select Users.Name,Posts.Title,Posts.Content,Posts.Date,Post.Tags,Categories.Name from Users,Posts,Categories where Users.ID==Posts.Author and Posts.Title like ? and Categories.ID==Posts.Category",(match.group(1),)) if post not in ret)
  34.                     continue
  35.                 match = self.isTag.match(case)
  36.                 if match is not None:
  37.                     ret.extend(post for post in self.cursor.execute("Select Users.Name,Posts.Title,Posts.Content,Posts.Date,Post.Tags,Categories.Name from Users,Posts,Categories where Users.ID==Posts.Author and Posts.Tags like ? and Categories.ID==Posts.Category",('%{}%'.format(match.group(1)),)) if post not in ret)
  38.                     continue
  39.                 match = self.isCat.match(case)
  40.                 if match is not None:
  41.                     ret.extend(post for post in self.cursor.execute("Select Users.Name,Posts.Title,Posts.Content,Posts.Date,Post.Tags,Categories.Name from Users,Posts,Categories where Users.ID==Posts.Author and Categories.Name like ? and Categories.ID==Posts.Category",(match.group(1),)) if post not in ret)
  42.                     continue
  43.         return ret
  44.  
  45. if __name__ == '__main__':
  46.     print("Py-Blog module v{}.{}.{}".format(major,minor,bugfix))
  47.     for post in Blog().Posts(input("Sortment> ")):
  48.         print("{} by {}".format(post['title'],post['user']))
  49.         print(post['content'])
  50.         print("Written on {} in categories: {}".format(post['date'],post['cat']))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement