使用python脚本恢复原md文件

Posted in 2016-7-28 23:56 | Category: Python | Tags: pelican python

首先,这个恢复不是从磁盘中恢复,而是从数据库中恢复

原因呢,不久前我的博客md文件被我不小心删了, 而我的博客分静态博客(基于pelican),和我自己写的动态博客(需数据库),静态博客需要md文件, 不过还好,我的数据库一直有备份,所以直接从数据库中恢复

直接给出代码

import psycopg2
import os

conn = psycopg2.connect(database="blog",
                        user="postgres",
                        password="Your password",
                        host="127.0.0.1",
                        port="5432")
cur = conn.cursor()
cur.execute('select id,title,publish,author,category,content from articles')
articles = cur.fetchall()
print(len(articles))
for article in articles:
    cur.execute("select a.title,t.name from articles as a,tags as t, \
    tag_article as tt where  \
    tt.tags_id = t.id and tt.articles_id = a.id and a.id = %d" %
                article[0])
    tags = cur.fetchall()
    tags = ','.join(tag[1] for tag in tags)

    filename = os.path.join('markdown/', '%s.md' % article[1])
    title = (
        'Title: %s\nAuthor: %s\nDate: %s\nCategory: %s\nTags: %s\nSlug: %s\nSummary: %s\n …

使用pelican搭建个人博客

Posted in 2015-12-22 8:48 | Category: Python | Tags: pelican python

pelican介绍

Pelican是一个用Python语言编写的静态网站生成器,支持使用restructuredText和Markdown写文章,配置灵活,扩展性强

pelican安装

$ sudo pip install pelican

安装markdown

$ sudo pip install markdown

pelican使用

工具准备好了,接下来就开始使用

$ cd git
$ mkdir pelican  #建立一个文件夹(位置和名称随意,自己记得就行)
$ cd pelican
$ pelican-quickstart

显示 (也可以直接回车默认)

Welcome to pelican-quickstart v3.4.0.

This script will help you create a new Pelican-based website.

Please answer the following questions so this script can generate the files
needed by Pelican.


> Where do you want to create your new web site? [.]
> What will be the title of this web site? HonMaple
> Who will be the author of this web site? honmaple
> What will be the default language of this web site? [en] zh
> Do you want to specify a URL prefix? e.g., http://example.com   (Y/n)
> What is your URL prefix? (see above example; no trailing …