Skip to content

Instantly share code, notes, and snippets.

@Lonami
Lonami / keybase.md
Created Jun 15, 2019
Keybase proof
View keybase.md

Keybase proof

I hereby claim:

  • I am lonami on github.
  • I am lonami (https://keybase.io/lonami) on keybase.
  • I have a public key ASCoYKcU4x-Wl1k9AbYqLywxX2OUiE5XxHIfGn7Z17QheAo

To claim this, I am signing this object:

@Lonami
Lonami / find-dupes.py
Created May 9, 2019
Find duplicated photo files with Telethon
View find-dupes.py
import collections
from io import BytesIO
import imagehash
from telethon.tl import types
from PIL import Image
chat = 'https://t.me/WallpaperWay'
hashes = collections.defaultdict(list)
@Lonami
Lonami / results.txt
Created Apr 27, 2019
Benchmarking different ways to concatenate byte strings in Python and comparing the results
View results.txt
concat byte strings 0.208433858000717
bad f-string (no encoding) 0.37026700899969
f-string with encoding 0.5117196850005712
global buffer 0.3610776010000336
hacker-man 0.550181579999844
bytes 0.37172514800022327
bytearray 0.4309359079998103
b join 0.18783723399974406 <- winner
b join list 0.21064743200076919
b join appnd 0.46364543099934963
@Lonami
Lonami / get-build-python.sh
Created Apr 24, 2019
Get the latest Python version, build it and install it
View get-build-python.sh
# Adapted from https://superuser.com/a/1412976
CPYTHON="$HOME/Documents/cpython"
PREFIX="/usr/local/python$1"
REQUIRED="zlib1g-dev libffi-dev libssl-dev"
OPTIONAL="libbz2-dev libncursesw5-dev libgdbm-dev liblzma-dev libsqlite3-dev tk-dev uuid-dev libreadline-dev"
if [ $# -ne 1 ]; then
echo "Usage: $0 <python version number>" >&2
exit 1
fi
View bridge-and-torch.py
import itertools
def _step(src, dst, mov, go, cst, cost, best, sols, slice):
"""
src : list of people in the source position.
dst : list of people in the destination.
mov : list of movements made.
go : are we going from src to dst, or not (coming from dst to src)?
cost : mapping from items in src to their cost.
@Lonami
Lonami / get-telegram-bot-tokens.py
Created Mar 11, 2019
Get the tokens from all your Telegram bots
View get-telegram-bot-tokens.py
from telethon.sync import TelegramClient
session = ...
api_id = ...
api_hash = ...
with TelegramClient(session, api_id, api_hash) as client:
print('bots = {')
seen = {b'bots?o=0'}
@Lonami
Lonami / check.py
Last active Apr 6, 2020
Check who can run Telegram methods (user, bot, both)
View check.py
# You should create a bot in the test servers.
# In Telegram Desktop, enter settings and type "testmode"
# Sign in with your phone number, since public phone numbers cannot create bots.
# Create a bot and grab the token.
# You can use a public test number to run as an user.
import csv
import random
from telethon import TelegramClient, functions, errors, sync
from telethon.tl import TLRequest
@Lonami
Lonami / filetype.py
Last active Dec 22, 2020
Recognize common filetypes with just their first 12 bytes
View filetype.py
def filetype(file):
with open(file, 'rb') as f:
chunk = f.read(16)
zero = chunk[:4] # There are many comparisions at offset 0
if zero == b'\x89PNG':
if chunk[4:8] == b'\r\n\x1a\n':
return 'png'
if zero == b'\xff\xd8\xff\xe0' and chunk[6:11] == b'JFIF\0':
@Lonami
Lonami / dump-media-channel.py
Created Oct 23, 2018
Dumps a Telegram media channel that contains only photos
View dump-media-channel.py
# MAKE SURE TO PASS THE SAME CHANNEL NAME EVERY TIME
import sys
import os, time, asyncio
from telethon import TelegramClient, events, utils, types, functions, sync
ch = sys.argv[1]
env = os.environ
client = TelegramClient(env['TG_SESSION'], env['TG_API_ID'], env['TG_API_HASH']).start()
@Lonami
Lonami / dump-media-channel.py
Created Oct 23, 2018
Dumps a Telegram media channel that contains only photos
View dump-media-channel.py
# MAKE SURE TO PASS THE SAME CHANNEL NAME EVERY TIME
import sys
import os, time, asyncio
from telethon import TelegramClient, events, utils, types, functions, sync
ch = sys.argv[1]
env = os.environ
client = TelegramClient(env['TG_SESSION'], env['TG_API_ID'], env['TG_API_HASH']).start()
You can’t perform that action at this time.