-
Notifications
You must be signed in to change notification settings - Fork 0
/
lvstats.py
58 lines (43 loc) · 1.36 KB
/
lvstats.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import pywikibot, re, os, requests, sys
import toolforge
from datetime import date, datetime, timedelta, timezone
from pytz import timezone
#from customFuncs import basic_petscan
from natsort import natsorted
import pymysql
utc_timezone = timezone("UTC")
lva_timezone = timezone("Europe/Riga")
conn = toolforge.toolsdb('s53143__meta_p')
conn1 = toolforge.connect('lvwiki_p')
def utc_to_local(utc_dt):
return utc_timezone.localize(utc_dt).astimezone(lva_timezone)
#
def local_to_utc(utc):
return lva_timezone.localize(utc).astimezone(utc_timezone)
#
def encode_if_necessary(b):
if type(b) is bytes:
return b.decode('utf8')
return b
def run_query(query,connection):
#query = query.encode('utf-8')
#print(query)
try:
cursor = connection.cursor()
cursor.execute(query)
rows = cursor.fetchall()
except KeyboardInterrupt:
sys.exit()
return rows
#
file = run_query('select count(*) from page where page_is_redirect=0 and page_namespace=0',conn1)
file = encode_if_necessary(file[0][0])
#http://pymysql.readthedocs.io/en/latest/user/examples.html
cursor = conn.cursor()
localtime2 = utc_to_local(datetime.utcnow())
dateforq12 = "{0:%Y%m%d%H%M%S}".format(localtime2)
sql2 = 'INSERT INTO `lvstats__stats` (`timest`, `articles`) VALUES (%s, %s)'
cursor.execute(sql2, (dateforq12,file))
#
conn.commit()
conn.close()