PEP 8 compliance

This commit is contained in:
Simon 2018-12-01 10:57:14 -05:00
parent bc4094923a
commit 433aa66a10

View File

@ -1,9 +1,7 @@
#!/usr/bin/python #!/usr/bin/python
import discord import discord
import asyncio
import getpass import getpass
import argparse import argparse
import re
import logging import logging
logging.basicConfig( logging.basicConfig(
@ -16,29 +14,49 @@ log = logging.getLogger(__name__)
parser = argparse.ArgumentParser(description='Scrapes messages from a Discord channel.') parser = argparse.ArgumentParser(description='Scrapes messages from a Discord channel.')
parser.add_argument('--username','-u', action='store', help='Username to login under. If not specified, username will be prompted for.') parser.add_argument('--username', '-u', action='store', help='Username to login under. If not specified, '
#parser.add_argument('--password','-p', action='store', help='Password to login under. If not specified, password will be prompted for.') 'username will be prompted for.')
parser.add_argument('--flag','-f', action='store', default="!yank", help='An alternative to specifing the server and channel, specify a piece of regex which when matched against a message sent by the target user, will trigger scraping of the channel the message was posted in. Useful for private messages and private chats. Default value is "!yank", activates by default if no server is specified.') # parser.add_argument('--password','-p', action='store', help='Password to login under. If not specified,
parser.add_argument('--quiet','-q', action='store_true', help='Supress messages in Discord') # password will be prompted for.')
parser.add_argument('--server','--guild','-s', action='store', help='Discord server name to scrape from (user must be a member of the server and have history privileges). This field is case sensitive. If channel is not specified the entire server will be scraped.') parser.add_argument('--flag', '-f', action='store', default="!yank", help='An alternative to specifying the server and'
parser.add_argument('--channel','-c', action='store', help='Discord channel name to scrape from (user must have history privileges for the particular channel). This field is case sensitive.') ' channel, specify a piece of regex which'
parser.add_argument('--limit','-l', action='store', default=1000000, type=int, help='Number of messages to save. Default is 1000000') ' when matched against a message sent by the'
parser.add_argument('--output','-o', action='store', help="Outputs all messages into a single file. If not specified, messages are saved under the format: <channel name>.txt.") ' target user, will trigger scraping of the'
parser.add_argument('--logging', action='store', choices=[10,20,30,40,50], default=20, help='Change the logging level. Defaults to 20, info.') ' channel the message was posted in. Useful'
' for private messages and private chats.'
' Default value is "!yank", activates by'
' default if no server is specified.')
parser.add_argument('--quiet', '-q', action='store_true', help='Suppress messages in Discord')
parser.add_argument('--server', '--guild','-s', action='store', help='Discord server name to scrape from '
'(user must be a member of the server and'
' have history privileges). This field is case'
' sensitive. If channel is not specified the '
'entire server will be scraped.')
parser.add_argument('--channel', '-c', action='store', help='Discord channel name to scrape from '
'(user must have history privileges for the particular'
' channel). This field is case sensitive.')
parser.add_argument('--limit', '-l', action='store', default=1000000, type=int, help='Number of messages to save.'
' Default is 1000000')
parser.add_argument('--output', '-o', action='store', help="Outputs all messages into a single file."
" If not specified, messages are saved under the format:"
" <channel name>.txt.")
parser.add_argument('--logging', action='store', choices=[10, 20, 30, 40, 50], default=20, help='Change the logging '
'level. Defaults to 20, info.')
args = parser.parse_args() args = parser.parse_args()
log.setLevel(args.logging) log.setLevel(args.logging)
# prompt for username # prompt for username
if (not args.username): if not args.username:
args.username = input("Username: ") args.username = input("Username: ")
password = getpass.getpass("Password for user {0}: ".format(args.username)) password = getpass.getpass("Password for user {0}: ".format(args.username))
client = discord.Client() client = discord.Client()
async def getLogs(channel):
async def get_logs(channel):
try: try:
if not args.quiet: if not args.quiet:
await client.send_message(channel, "Getting the logs for channel {0}".format(channel.name)) await client.send_message(channel, "Getting the logs for channel {0}".format(channel.name))
@ -67,13 +85,14 @@ async def getLogs(channel):
@client.async_event @client.async_event
async def on_message(message): async def on_message(message):
try: try:
log.debug(str(message.channel.server.name) + " -> " + str(message.channel.name) + ' - ' + str(message.author) + ': ' + str(message.content)) log.debug(str(message.channel.server.name) + " -> " + str(message.channel.name) + ' - ' + str(message.author) +
': ' + str(message.content))
except: except:
log.debug("Private message - " + str(message.author) + ': ' + str(message.content)) log.debug("Private message - " + str(message.author) + ': ' + str(message.content))
if not args.server and not args.channel: if not args.server and not args.channel:
if args.flag == message.content[:len(args.flag)]: if args.flag == message.content[:len(args.flag)]:
await getLogs(message.channel) await get_logs(message.channel)
# if (not args.server) and message.author.id == client.user.id and re.compile(args.flag).match(message.content): # if (not args.server) and message.author.id == client.user.id and re.compile(args.flag).match(message.content):
# print("Matched {}".format(args.flag)) # print("Matched {}".format(args.flag))
# await getLogs(message.channel) # await getLogs(message.channel)
@ -84,20 +103,19 @@ async def on_ready():
log.info("Logged in as user {0}".format(client.user.name)) log.info("Logged in as user {0}".format(client.user.name))
if args.server and args.channel: if args.server and args.channel:
channel = ""
try: try:
channel = discord.utils.get(client.get_all_channels(), server__name=args.server, name=args.channel) channel = discord.utils.get(client.get_all_channels(), server__name=args.server, name=args.channel)
except: except:
channel = "" channel = ""
if channel: if channel:
await getLogs(channel) await get_logs(channel)
else: else:
log.error("Could not find channel {0} in server {1}".format(args.channel, args.server)) log.error("Could not find channel {0} in server {1}".format(args.channel, args.server))
await client.logout() await client.logout()
elif args.server: elif args.server:
log.info("Downloading messages for all channels in server {0}".format(arg.server)) log.info("Downloading messages for all channels in server {0}".format(args.server))
for channel in server.channels: for channel in server.channels:
await getLogs(channel) await get_logs(channel)
await client.logout() await client.logout()
else: else:
log.info('Entering flag mode with flag "{0}"'.format(args.flag)) log.info('Entering flag mode with flag "{0}"'.format(args.flag))