python wordpress 文章入数据库_Python 直接导入WordPress数据库

import MySQLdb

import datetime

import time

from  tools import *

def wp_checktitle(dbconn,title):

'''wordpress检测是否有重复标题'''

cursor=dbconn.cursor()

sql = "select post_title from wp_posts where post_title='%s'" % (title)

cursor.execute(sql)

if cursor.rowcount == 0:

checkflag = 1

else:

checkflag = 0

return checkflag

def  sync_wordpress(dbconn,title,content):

'''同步wordpress程序'''

checkflag = wp_checktitle(dbconn,title)

cursor=dbconn.cursor()

curtime = str(datetime.datetime.now())[:19]

post_author = 1

post_date = curtime

post_date_gmt = curtime

post_content = content

post_title = title

post_name = post_title

post_modified = curtime

post_modified_gmt = curtime

post_content_filtered = ''

currenttime = int(time.time())

if checkflag:

try:

postsql = ''

postsql = '''INSERT INTO `wp_posts` (

`post_author` ,

`post_date` ,

`post_date_gmt` ,

`post_content` ,

`post_title` ,

`post_name` ,

`post_modified`,

`post_modified_gmt`,

`post_content_filtered`

)

VALUES (

'%(post_author)s','%(post_date)s','%(post_date_gmt)s','%(post_content)s','%(post_title)s','%(post_name)s','%(post_modified)s','%(post_modified_gmt)s','%(post_content_filtered)s')''' % {'post_author':post_author,'post_date':post_date,'post_date_gmt':post_date_gmt,'post_content':post_content,'post_title':post_title,'post_name':post_name,'post_modified':post_modified,'post_modified_gmt':post_modified_gmt,'post_content_filtered':post_content_filtered}

cursor.execute(postsql)

dbconn.commit()

rowid = cursor.lastrowid

metasql = ''

metasql = "insert into `wp_postmeta`(`post_id`)VALUES(%s)" % (rowid)

cursor.execute(metasql)

dbconn.commit()

insertsql = '''INSERT INTO `wp_term_relationships` (

`object_id` ,

`term_taxonomy_id`

)

VALUES (

%(object_id)s, %(term_taxonomy_id)s) ''' % {'object_id':rowid,'term_taxonomy_id':1}

cursor.execute(insertsql)

dbconn.commit()

return 1

except Exception, e:

print '数据库错误:', e

return 0

finally:

cursor.close()

dbconn.close()

else:

print 'wordpress title exist'

return 1

title = 'titl-wptitle'

zcontent = 'content—————–'

curhost = ''##远程数据库服务器地址

webuser = ''#数据库用户名

webpwd =''#数据库密码

webdb = ''#数据库名称

dbconn = MySQLdb.connect(host=curhost, user=webuser, passwd=webpwd, db=webdb,port=3306, charset='utf8')

flag = sync_wordpress(dbconn,title,zcontent)

if flag:

print 'wordpress sync success'

else:

print 'wordpress sync error'

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值