def __init__(self, config='config.json', apiver='v6'): """Discord constructor. :param config: The configuration JSON file. :param apiver: The current Discord API version. """ with open(config, 'r') as configfile: configdata = loads(configfile.read()) cfg = type('DiscordConfig', (object, ), configdata)() if cfg.token == "" or cfg.token is None: error('You must have an authorization token set in %s' % config) exit(-1) self.api = apiver self.buffer = cfg.buffer self.headers = {'user-agent': cfg.agent, 'authorization': cfg.token} self.types = cfg.types self.query = create_query_body(images=cfg.query['images'], files=cfg.query['files'], embeds=cfg.query['embeds'], links=cfg.query['links'], videos=cfg.query['videos'], nsfw=cfg.query['nsfw']) self.directs = cfg.directs if len(cfg.directs) > 0 else {} self.servers = cfg.servers if len(cfg.servers) > 0 else {} # Save us the time by exiting out when there's nothing to scrape. if len(cfg.directs) == 0 and len(cfg.servers) == 0: error('No servers or DMs were set to be grabbed, exiting.') exit(0)
def get_channel_name(self, channelid, isdm=False): """Get the channel name by its ID. :param channelid: The channel ID. :param isdm: A flag to check whether we're in a DM or not. """ if isdm: return channelid request = SimpleRequest(self.headers).request channel = request.grab_page('https://discordapp.com/api/%s/channels/%s' % (self.api, channelid)) if channel is not None and len(channel) > 0: return '%s_%s' % (channelid, safe_name(channel['name'])) else: error('Unable to fetch channel name from id, generating one instead.') return '%s_%s' % (channelid, random_str(12))
def get_server_name(self, serverid, isdm=False): """Get the server name by its ID. :param serverid: The server ID. :param isdm: A flag to check whether we're in a DM or not. """ if isdm: return serverid request = SimpleRequest(self.headers).request server = request.grab_page('https://discordapp.com/api/%s/guilds/%s' % (self.api, serverid)) if server is not None and len(server) > 0: return '%s_%s' % (serverid, safe_name(server['name'])) else: error('Unable to fetch server name from id, generating one instead.') return '%s_%s' % (serverid, random_str(12))