Skip to content

Instantly share code, notes, and snippets.

@EYHN
Created April 15, 2018 17:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EYHN/647020ef405654c9f054496bcf10437a to your computer and use it in GitHub Desktop.
Save EYHN/647020ef405654c9f054496bcf10437a to your computer and use it in GitHub Desktop.
telegram 频道热门信息排名,根据阅读数量排名
from telethon import TelegramClient
from telethon.tl.functions.messages import GetMessagesViewsRequest
import socks
import sys, time
# Open http://telethon.readthedocs.io/en/latest/extra/basic/creating-a-client.html#creating-a-client and login with your phone number.
# Click under API Development tools.
# A Create new application window will appear. Fill in your application details. There is no need to enter any URL, and only the first two fields (App title and Short name) can currently be changed later.
# Click on Create application at the end. Remember that your API hash is secret and Telegram won’t let you revoke it. Don’t post it anywhere!
api_id = 123456
api_hash = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
channel_name = '@awesomeopensource'
# proxy = (socks.SOCKS5, 'localhost', 1086)
proxy = None
client = TelegramClient('message_ranking', api_id, api_hash, proxy)
client.start()
messages = []
_messages = []
while True:
offset_id = len(_messages) == 100 and _messages[99].id or 0
_messages = client.get_message_history(
channel_name, limit=100, offset_id=offset_id)
sys.stdout.write('.')
sys.stdout.flush()
messages.extend(_messages)
if len(_messages) != 100:
break
time.sleep(0.5) # prevents FloodWaitError
sys.stdout.write('\n')
views = client(GetMessagesViewsRequest(
peer=client.get_entity(channel_name),
id=[message.id for message in messages],
increment=False
))
ranks = []
for i in range(len(views)):
ranks.append({'views': views[i], 'message': messages[i].message})
ranks = sorted(ranks, key = lambda item: item['views'])
for item in ranks:
print(str(item['views']) + '\t' + (item['message'] or ''))
print('-----------------------------------------')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment