Skip to content
This repository has been archived by the owner on Aug 23, 2020. It is now read-only.

Commit

Permalink
增加文本模式显示二维码、感谢@xrdavies
Browse files Browse the repository at this point in the history
  • Loading branch information
pandolia committed Apr 2, 2017
1 parent e674010 commit 921fc6f
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 26 deletions.
10 changes: 7 additions & 3 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -275,11 +275,11 @@ SmartQQ 登录时需要用手机 QQ 扫描二维码图片,在 QQBot 中,二
* GUI模式: 在 GUI 界面中自动弹出二维码图片
* 邮箱模式: 将二维码图片发送到指定的邮箱
* 服务器模式: 在一个 HTTP 服务器中显示二维码图片
* 文本模式:在Term中以文本形式展示二维码
* 文本模式: 在Term中以文本形式展示二维码

GUI 模式是默认的模式,只适用于个人电脑。邮箱模式可以适用于个人电脑和远程服务器。服务器模式一般只在有公网ip的系统中使用。如果使用 QQ 邮箱来接收二维码,则当发送二维码图片后,手机 QQ 客户端会立即收到通知,在手机 QQ 客户端上打开邮件,再长按二维码就可以扫描了。文本模式方便在开发过程或者服务器部署时使用,为开发者提供快捷方式登陆QQ
GUI 模式是默认的模式,只适用于个人电脑。邮箱模式可以适用于个人电脑和远程服务器。服务器模式一般只在有公网ip的系统中使用。如果使用 QQ 邮箱来接收二维码,则当发送二维码图片后,手机 QQ 客户端会立即收到通知,在手机 QQ 客户端上打开邮件,再长按二维码就可以扫描了。文本模式只适用于不带图形界面的系统,方便在开发过程或者服务器部署时使用,为开发者提供快捷方式登陆 QQ (注意:文本模式不适用于有图形界面的 terminal ,如 gnome-terminal , mate-terminal )

注意:当开启了邮箱模式或服务器模式时, GUI 模式是关闭的,登陆时不会自动弹出二维码图片。
注意:当开启了 邮箱模式/服务器模式/文本模式 时, GUI 模式是关闭的,登陆时不会自动弹出二维码图片。

每次登录时会创建一个二维码管理器 (QrcodeManager 对象) ,二维码管理器会根据配置文件及命令行参数来选择二维码图片的显示方式。

Expand Down Expand Up @@ -314,6 +314,9 @@ GUI 模式是默认的模式,只适用于个人电脑。邮箱模式可以适

# 该邮箱的 IMAP/SMTP 服务授权码
"mailAuthCode" : "feregfgftrasdsew",

# 是否以文本模式显示二维码
"cmdQrcode" : False,
# 显示/关闭调试信息
"debug" : False,
Expand Down Expand Up @@ -358,6 +361,7 @@ GUI 模式是默认的模式,只适用于个人电脑。邮箱模式可以适
# "qq" : "",
# "mailAccount" : "",
# "mailAuthCode" : "",
# "cmdQrcode" : False,
# "debug" : False,
# "restartOnOffline" : False,
# "fetchInterval" : 120,
Expand Down
2 changes: 1 addition & 1 deletion main.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from qqbot import Main; Main(cmdQrCode=False)
from qqbot import Main; Main()
10 changes: 5 additions & 5 deletions qqbot/basicqsession.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ class RequestError(Exception):

class BasicQSession(object):

def Login(self, conf, cmdQrCode):
def Login(self, conf):
self.prepareSession()
self.waitForAuth(conf, cmdQrCode)
self.waitForAuth(conf)
self.getPtwebqq()
self.getVfwebqq()
self.getUinAndPsessionid()
Expand Down Expand Up @@ -70,10 +70,10 @@ def getQrcode(self):
INFO('已获取二维码')
return qrcode

def waitForAuth(self, conf, cmdQrCode):
def waitForAuth(self, conf):
qrcodeManager = QrcodeManager(conf)
try:
qrcodeManager.Show(self.getQrcode(), cmdQrCode=cmdQrCode)
qrcodeManager.Show(self.getQrcode())
x, y = 1, 1
while True:
time.sleep(3)
Expand All @@ -88,7 +88,7 @@ def waitForAuth(self, conf, cmdQrCode):
y = 0
elif '二维码已失效' in authStatus:
WARN('二维码已失效, 重新获取二维码')
qrcodeManager.Show(self.getQrcode(), cmdQrCode=cmdQrCode)
qrcodeManager.Show(self.getQrcode())
x, y = 1, 1
elif '登录成功' in authStatus:
INFO('已获授权')
Expand Down
35 changes: 32 additions & 3 deletions qqbot/qconf.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
# 该邮箱的 IMAP/SMTP 服务授权码
"mailAuthCode" : "feregfgftrasdsew",
# 是否以文本模式显示二维码
"cmdQrcode" : False,
# 显示/关闭调试信息
"debug" : False,
Expand Down Expand Up @@ -79,6 +82,7 @@
# "qq" : "",
# "mailAccount" : "",
# "mailAuthCode" : "",
# "cmdQrcode" : False,
# "debug" : False,
# "restartOnOffline" : False,
# "fetchInterval" : 120,
Expand All @@ -98,6 +102,7 @@
"qq" : "",
"mailAccount" : "",
"mailAuthCode" : "",
"cmdQrcode" : False,
"debug" : False,
"restartOnOffline" : False,
"fetchInterval" : 120,
Expand Down Expand Up @@ -163,6 +168,7 @@
-nr, --norestart 在掉线时不要重新启动。
其他:
-cq, --cmdQrcode 以文本模式显示二维码
-fi FETCHINTERVAL, --fetchInterval FETCHINTERVAL
设置每轮联系人列表更新之间的间歇时间(单位:秒)。
-saf, --startAfterFetch 全部联系人资料获取完成后再启动 QQBot
Expand Down Expand Up @@ -195,24 +201,46 @@ def __init__(self, qq=None, user=None):

def readCmdLine(self):
parser = argparse.ArgumentParser(add_help=False)

parser.add_argument('-h', '--help', action='store_true')

parser.add_argument('-u', '--user')

parser.add_argument('-q', '--qq')

parser.add_argument('-p', '--termServerPort', type=int)

parser.add_argument('-ip', '--httpServerIP')

parser.add_argument('-hp', '--httpServerPort', type=int)

parser.add_argument('-m', '--mailAccount')
parser.add_argument('-mc', '--mailAuthCode')
parser.add_argument('-d', '--debug', action='store_true', default=None)

parser.add_argument('-mc', '--mailAuthCode')

parser.add_argument('-cq', '--cmdQrcode',
action='store_true', default=None)

parser.add_argument('-d', '--debug',
action='store_true', default=None)

parser.add_argument('-nd', '--nodebug', action='store_true')

parser.add_argument('-r', '--restartOnOffline',
action='store_true', default=None)
parser.add_argument('-nr', '--norestart', action='store_true')

parser.add_argument('-nr', '--norestart',
action='store_true')

parser.add_argument('-fi', '--fetchInterval', type=int)

parser.add_argument('-saf', '--startAfterFetch',
action='store_true', default=None)

parser.add_argument('-mt', '--monitorTables')

parser.add_argument('-pp', '--pluginPath')

parser.add_argument('-pl', '--plugins')

try:
Expand Down Expand Up @@ -340,6 +368,7 @@ def Display(self):
self.httpServerIP and self.httpServerPort or '无')
INFO('用于接收二维码的邮箱账号:%s', self.mailAccount or '无')
INFO('邮箱服务授权码:%s', self.mailAccount and '******' or '无')
INFO('以文本模式显示二维码:%s', self.cmdQrcode and '是' or '否')
INFO('调试模式:%s', self.debug and '开启' or '关闭')
INFO('掉线后自动重启:%s', self.restartOnOffline and '是' or '否')
INFO('每轮联系人列表刷新之间的间歇时间:%d 秒', self.fetchInterval)
Expand Down
12 changes: 6 additions & 6 deletions qqbot/qqbotcls.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from qqbot.mainloop import MainLoop, Put
from qqbot.groupmanager import GroupManager

def runBot(botCls, qq, user, cmdQrCode):
def runBot(botCls, qq, user):
if sys.argv[-1] == '--subprocessCall':
isSubprocessCall = True
sys.argv.pop()
Expand All @@ -33,7 +33,7 @@ def runBot(botCls, qq, user, cmdQrCode):

if isSubprocessCall:
bot = botCls()
bot.Login(qq, user, cmdQrCode)
bot.Login(qq, user)
bot.Run()
else:
conf = QConf(qq, user)
Expand Down Expand Up @@ -65,16 +65,16 @@ def runBot(botCls, qq, user, cmdQrCode):
else:
sys.exit(code)

def RunBot(botCls=None, qq=None, user=None, cmdQrCode=False):
def RunBot(botCls=None, qq=None, user=None):
try:
runBot((botCls or QQBot), qq, user, cmdQrCode)
runBot((botCls or QQBot), qq, user)
except KeyboardInterrupt:
sys.exit(1)

class QQBot(GroupManager):

def Login(self, qq=None, user=None, cmdQrCode=False):
session, contactdb, self.conf = QLogin(qq, user, cmdQrCode)
def Login(self, qq=None, user=None):
session, contactdb, self.conf = QLogin(qq, user)

# main thread
self.SendTo = session.SendTo
Expand Down
18 changes: 12 additions & 6 deletions qqbot/qrcodemanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,23 @@ def __init__(self, conf):

else:
self.mailAgent = None

self.cmdQrcode = conf.cmdQrcode

def Show(self, qrcode, cmdQrCode=False):
def Show(self, qrcode):
with open(self.qrcodePath, 'wb') as f:
f.write(qrcode)

if self.qrcodeServer is None and self.mailAgent is None:
if self.cmdQrcode:
try:
if cmdQrCode:
showCmdQRCode(self.qrcodePath)
else:
showImage(self.qrcodePath)
showCmdQRCode(self.qrcodePath)
except Exception as e:
WARN('无法以文本模式显示二维码图片 file://%s 。%s',
self.qrcodePath, e)

if not (self.qrcodeServer or self.mailAgent or self.cmdQrcode):
try:
showImage(self.qrcodePath)
except Exception as e:
WARN('无法弹出二维码图片 file://%s 。%s', self.qrcodePath, e)

Expand Down
4 changes: 2 additions & 2 deletions qqbot/qsession.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from qqbot.common import JsonDumps, HTMLUnescape, PY3
from qqbot.groupmanager import GroupManagerSession

def QLogin(qq=None, user=None, cmdQrcode=False):
def QLogin(qq=None, user=None):
conf = QConf(qq, user)
conf.Display()

Expand Down Expand Up @@ -42,7 +42,7 @@ def QLogin(qq=None, user=None, cmdQrcode=False):

INFO('开始手动登录...')
session = QSession()
session.Login(conf, cmdQrcode)
session.Login(conf)
contactdb = QContactDB(session, conf.PicklePath())
contactdb.Dump()
return session.Copy(), contactdb, conf
Expand Down

0 comments on commit 921fc6f

Please sign in to comment.