-
Notifications
You must be signed in to change notification settings - Fork 2
/
another_test.py
125 lines (105 loc) · 4.55 KB
/
another_test.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
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
# vim: set expandtab tabstop=4 shiftwidth=4 :
import ConfigParser
from twitterbot import TwitterBot
import logging
from optparse import OptionParser
import os
import sys
""" Figure out what to set the logging level to. There isn't a
straightforward way of doing this because Python uses constants that are
actually integers under the hood, and I'd really like to be able to do
something like loglevel = 'logging.' + loglevel. I can't have a pony,
either. Takes a string, returns a Python loglevel. """
def process_loglevel(loglevel):
if loglevel == 'critical':
return 50
if loglevel == 'error':
return 40
if loglevel == 'warning':
return 30
if loglevel == 'info':
return 20
if loglevel == 'debug':
return 10
if loglevel == 'notset':
return 0
# Core code...
if __name__ == '__main__':
# If we're running in a Python environment earlier than v3.0, set the
# default text encoding to UTF-8 because XMPP requires it.
if sys.version_info < (3, 0):
reload(sys)
sys.setdefaultencoding('utf-8')
# Determine the name of this bot from its filename (without the file
# extension, if there is one).
botname = os.path.basename(__file__).split('.')[0]
# Instantiate a command line options parser.
optionparser = OptionParser()
# Define command line switches for the bot, starting with being able to
# specify an arbitrary configuration file for a particular bot.
optionparser.add_option('-c', '--conf', dest='configfile', action='store',
type='string', help='Specify an arbitrary config file for this bot. Defaults to botname.conf.')
# Add a command line option that lets you override the config file's
# loglevel. This is for kicking a bot into debug mode without having to
# edit the config file.
optionparser.add_option('-l', '--loglevel', dest='loglevel', action='store',
help='Specify the default logging level of the bot. Choose from CRITICAL, ERROR, WARNING, INFO, DEBUG, NOTSET. Defaults to INFO.')
# Parse the command line args.
(options, args) = optionparser.parse_args()
# Read its unique configuration file. There is a command line argument
# for specifying a configuration file, but it default to taking the name
# of the bot and appending '.conf' to it. Then load it into a config file
# parser object.
config = ConfigParser.ConfigParser()
if options.configfile:
config.read(options.configfile)
else:
config.read(botname + '.conf')
# Get configuration options out of whichever configuration file gets used.
owner = config.get(botname, 'owner')
username = config.get(botname, 'username')
password = config.get(botname, 'password')
muc = config.get(botname, 'muc')
muclogin = config.get(botname, 'muclogin')
imalive = config.get(botname, 'imalive')
function = config.get(botname, 'function')
# Figure out how to configure the logger. Start by reading the config
# file.
config_log = config.get(botname, 'loglevel').lower()
if config_log:
loglevel = process_loglevel(config_log)
# Then try the command line.
if options.loglevel:
loglevel = process_loglevel(options.loglevel.lower())
# Default to WARNING.
if not options.loglevel and not loglevel:
loglevel = logging.WARNING
# Configure the logger.
logging.basicConfig(level=loglevel,
format='%(levelname)-8s %(message)s')
# Get the filename of the response file from the config file.
responsefile = config.get(botname, 'responsefile')
# Determine which of the microblog classes to instantiate.
microblog_type = config.get(botname, 'type')
if microblog_type == 'twitter':
# Load the Twitter API keys from the config file.
api_key = config.get(botname, 'api_key')
api_secret = config.get(botname, 'api_secret')
access_token = config.get(botname, 'access_token')
access_token_secret = config.get(botname, 'access_token_secret')
# Instantiate a Twitter bot.
bot = TwitterBot(owner, botname, username, password, muc, muclogin,
imalive, responsefile, function, api_key, api_secret, access_token,
access_token_secret)
else:
print "No other kinds of microblog bots are defined yet."
sys.exit(0)
# Connect to the XMPP server and start processing messages.
if bot.connect():
bot.process(block=False)
else:
print "ERROR: Unable to connect to XMPP server."
sys.exit(1)
# Fin.