forked from detorto/tulen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtulen.py
executable file
·115 lines (87 loc) · 2.59 KB
/
tulen.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/python
# -*- coding: utf-8 -*-
#import vk
import urllib2
import requests
import pkgutil
import imp
import os
import threading
import json
import time
from vkuser import VkUser
import sys
import traceback
import io
from optparse import OptionParser
import yaml
import multiline_formatter
import logging
LOG_SETTINGS = {
'version': 1,
'handlers': {
'console': {
'class': 'logging.StreamHandler',
'level': 'INFO',
'formatter': 'default',
'stream': 'ext://sys.stdout',
},
},
'formatters': {
'default': {
'()': 'multiline_formatter.formatter.MultilineMessagesFormatter',
'format': '[%(levelname)s] %(message)s'
},
},
'loggers': {
'tulen': {
'level':'DEBUG',
'handlers': ['console',]
},
}
}
logging.config.dictConfig(LOG_SETTINGS)
logger = logging.getLogger("tulen")
def process(config, testmode):
def update_stat(stat, value):
stats_file_path = config.get("stats_file", "statistics.yaml");
try:
f = yaml.load(open(stats_file_path))
except:
f = None
if f:
upd = f.get(stat, 0)
upd += value;
f[stat] = upd
else:
f = {stat:1}
with io.open(stats_file_path, 'w', encoding='utf-8') as fo:
fo.write(unicode(yaml.dump(f)))
logger.info("Updated statistic: {} :+{}".format(stat,value))
vkuser = VkUser(config, update_stat, testmode)
logger.info("Created user api")
logger.info("Starting processing... ")
while True:
try:
vkuser.process_all_messages()
except:
logger.error("Something wrong while processin dialogs")
exc_type, exc_value, exc_traceback = sys.exc_info()
logger.error("\n".join(traceback.format_exception(exc_type, exc_value, exc_traceback)))
if testmode:
raise
continue
def main():
parser = OptionParser()
parser.add_option("-c", "--config", dest="config",
help="configuration file to use", default="access.yaml", metavar="FILE.yaml")
parser.add_option("-t", "--test", dest="testmode",
help="test mode", action="store_true", default=False)
(options, args) = parser.parse_args()
logger.info ( "*************Tulen vk.com bot****************" )
config = yaml.load(open(options.config));
logger.info("Loaded configuration ")
logger.info(yaml.dump(config))
process(config, options.testmode)
if __name__ == '__main__':
main()