Skip to content

Instantly share code, notes, and snippets.

@ocus
Last active October 24, 2017 06:24
Show Gist options
  • Save ocus/cca27a7f0ec52116a599f96e58ad71b8 to your computer and use it in GitHub Desktop.
Save ocus/cca27a7f0ec52116a599f96e58ad71b8 to your computer and use it in GitHub Desktop.
import requests
import logging
from sys import version_info
if version_info[0] < 3:
import urllib
def encode_url_vars(vars):
return urllib.urlencode(vars)
else:
import urllib.parse
def encode_url_vars(vars):
return urllib.parse.urlencode(vars)
AUTHORISATION = 'un truc à la zob que tu trouves dans les headers http des requêtes'
CHANNEL = '369899114063986690'
logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.WARN)
logger = logging.getLogger(__name__)
def get_messages(channel, limit=100, before=None):
url_vars = {'limit': limit}
if before:
url_vars['before'] = before
url = 'https://discordapp.com/api/v6/channels/{channel}/messages?{url_vars}'.format(
channel=channel,
limit=limit,
url_vars=encode_url_vars(url_vars)
)
logger.info('Fetching URL %s', url)
r = requests.get(
url,
headers={
'authorization': AUTHORISATION
}
)
return r.json()
def count_unique_joins(channel):
unique_joins = 0
authors = set()
before = None
limit = 100
while True:
messages = get_messages(channel=channel, limit=limit, before=before)
unique_joins += len([
message for message in messages
if message['type'] == 7 and message['author']['id'] not in authors
])
authors.update([message['author']['id'] for message in messages])
if not len(messages) >= limit:
break
before = messages[-1]['id']
return unique_joins
count = count_unique_joins(channel=CHANNEL)
print("Unique joins:", count)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment