From 65093cf27807ac55477bcc12ea3ad2e2f67cdbc2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=BD=A6=E4=BA=AE=E4=BA=AE?= Date: Mon, 19 Dec 2016 21:33:23 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=BC=BA=E5=A4=B1=E6=96=87?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- blog/wordpress_helper.py | 44 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 blog/wordpress_helper.py diff --git a/blog/wordpress_helper.py b/blog/wordpress_helper.py new file mode 100644 index 0000000..6e0957b --- /dev/null +++ b/blog/wordpress_helper.py @@ -0,0 +1,44 @@ +#!/usr/bin/env python +# encoding: utf-8 + + +""" +@version: ?? +@author: liangliangyy +@license: MIT Licence +@contact: liangliangyy@gmail.com +@site: https://www.lylinux.org/ +@software: PyCharm +@file: wordpress_helper.py +@time: 2016/12/10 上午9:43 +""" + +import pymysql +import urllib +from urllib.parse import quote_plus, quote +import os + + +class wordpress_helper(): + def __init__(self): + USER = os.environ.get('DJANGO_MYSQL_USER') + PASSWORD = os.environ.get('DJANGO_MYSQL_PASSWORD') + HOST = os.environ.get('DJANGO_MYSQL_HOST') + self.db = pymysql.connect(HOST, USER, PASSWORD, 'djangoblog') + + def get_postid_by_postname(self, postname): + sql = "SELECT id from wordpress.wp_posts WHERE post_name='%s' " % quote(postname) + cursor = self.db.cursor() + cursor.execute(sql) + try: + result = cursor.fetchone() + return result[0] + except: + return 0 + + +if __name__ == '__main__': + name = '使用nginxgunicornvirtualenvsupervisor来部署django项目' + helper = wordpress_helper() + post_id = helper.get_postid_by_postname(name) + print(post_id)