Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- import sqlite3 as sqlite
- major = 1
- minor = 0
- bugfix = 0
- class Blog:
- isUser = re.compile(r'usr:\s*(.+)',re.IGNORECASE)
- isTitle = re.compile(r'title:\s*(.+)',re.IGNORECASE)
- isTag = re.compile(r'tag:\s*(.+)',re.IGNORECASE)
- isCat = re.compile(r'cat:\s*(.+)',re.IGNORECASE)
- def __init__(self):
- self._database = sqlite.connect('blog.db')
- self._database.row_factory = sqlite.Row
- self.cursor = self._database.cursor()
- 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")
- 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)")
- def Users(self,authors=True):
- if authors: return self.cursor.execute("Select ID, Name, Email from Users where Level > 1")
- return self.cursor.execute("Select ID, Name, Email from Users")
- def Posts(self,context='news'):
- ret = [] #Declare an initial array
- if context.lower() == 'news': ret = self.posts[0:10]
- else:
- for case in context.split(r'/'):
- match = self.isUser.match(case)
- if match is not None:
- 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)
- continue
- match = self.isTitle.match(case)
- if match is not None:
- 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)
- continue
- match = self.isTag.match(case)
- if match is not None:
- 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)
- continue
- match = self.isCat.match(case)
- if match is not None:
- 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)
- continue
- return ret
- if __name__ == '__main__':
- print("Py-Blog module v{}.{}.{}".format(major,minor,bugfix))
- for post in Blog().Posts(input("Sortment> ")):
- print("{} by {}".format(post['title'],post['user']))
- print(post['content'])
- print("Written on {} in categories: {}".format(post['date'],post['cat']))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement