2016-04-24 00:17:26 +08:00
# -*- coding: utf-8 -*-
#!/usr/bin/python
import json
2016-04-25 22:19:00 +08:00
import re
2016-04-24 00:17:26 +08:00
import sqlite3
import sys
2016-04-29 16:08:50 +08:00
import Global
2016-04-24 00:17:26 +08:00
reload ( sys )
sys . setdefaultencoding ( ' utf-8 ' )
2016-04-29 16:08:50 +08:00
file = open ( Global . content_dir )
2016-04-24 00:17:26 +08:00
conn = sqlite3 . connect ( ' news.db ' )
2019-09-26 11:55:32 +08:00
# Check table is exist
2019-10-10 00:26:39 +08:00
# Method 1
# cursor = conn.execute("SELECT count(*) FROM sqlite_master WHERE type='table' AND name='news';")
# result = cursor.fetchone()[0]
# Method 2
conn . execute ( " CREATE TABLE IF NOT EXISTS news (title, content, time, url) " )
conn . commit ( )
2019-09-26 11:55:32 +08:00
2016-04-24 00:17:26 +08:00
while 1 :
line = file . readline ( )
if not line :
break
2016-04-25 22:19:00 +08:00
line = re . sub ( " ' " , " ’ " , line )
2016-04-24 00:17:26 +08:00
data = json . loads ( line )
2019-10-10 00:26:39 +08:00
insertsql = " insert into news(title,content,time,url) values ( ' " + str ( data [ ' title ' ] ) . decode ( ' utf-8 ' ) + " ' , ' " + str ( data [ ' content ' ] ) + " ' , ' " + str ( data [ ' time ' ] ) . decode ( ' utf-8 ' ) + " ' , ' " + str ( data [ ' url ' ] ) . decode ( ' utf-8 ' ) + " ' ) "
print data [ ' title ' ] . decode ( ' utf-8 ' )
2016-04-24 00:17:26 +08:00
conn . execute ( insertsql )
conn . commit ( )
conn . close ( )