43 lines
1.3 KiB
Python
43 lines
1.3 KiB
Python
import psycopg2
|
|
import datetime
|
|
import time
|
|
import csv
|
|
|
|
con = psycopg2.connect(host = "localhost", port="5432", database='oschina', user='postgres', password='123456')
|
|
def getRatings():
|
|
data = []
|
|
try:
|
|
cur = con.cursor()
|
|
cur.execute('SELECT id,level,created_at,reply_count,view_count,click_count,'
|
|
'word_count from "iTags"."articles" where lang = %s' % repr("ZH"))
|
|
curs = cur.fetchall()
|
|
for item in curs:
|
|
temp = item
|
|
# print(temp)
|
|
data.append([temp[0],temp[1],temp[2],temp[3],temp[4],temp[5],temp[6]])
|
|
except psycopg2.DatabaseError as e:
|
|
print('Error %s' % e)
|
|
|
|
return data
|
|
|
|
def timeLong(time1,time2 = datetime.datetime.now(), type = "day"):
|
|
day_num = -1
|
|
if type == "day":
|
|
day_num = (int(time.mktime(time2)) - int(time.mktime(time1))) / (24 * 60 * 60)
|
|
if day_num >= 0:
|
|
return day_num
|
|
else:
|
|
return "error time"
|
|
|
|
def write2csv():
|
|
|
|
articles = getRatings()
|
|
with open("articles.csv","w") as csvfile:
|
|
csvwriter = csv.writer(csvfile)
|
|
csvwriter.writerow(["id","level","created_at","reply_count","view_count",
|
|
"click_count","word_count"])
|
|
for article in articles:
|
|
article[2] = article[2].date()
|
|
csvwriter.writerow(article)
|
|
|
|
write2csv() |