1
0
mirror of https://github.com/instaloader/instaloader.git synced 2024-08-17 12:19:38 +02:00

Added functions to convert mediaid <-> shortcode

This commit is contained in:
André Koch-Kramer 2017-07-06 22:26:25 +02:00
parent 5ae3d7090f
commit 184c521646

View File

@ -15,6 +15,7 @@ import sys
import tempfile
import time
from argparse import ArgumentParser
from base64 import b64decode, b64encode
from io import BytesIO
from typing import Any, Callable, Dict, List, Optional
@ -131,6 +132,21 @@ def get_anonymous_session() -> requests.Session:
return session
def shortcode_to_mediaid(code: str) -> int:
if len(code) > 11:
print("Wrong shortcode \"{0}\", unable to convert to mediaid.".format(code), file = sys.stderr)
return
code = 'A' * (12 - len(code)) + code
return int.from_bytes(b64decode(code.encode(), b'-_'), 'big')
def mediaid_to_shortcode(mediaid: int) -> str:
if mediaid.bit_length() > 64:
print("Wrong mediaid {0}, unable to convert to shortcode".format(str(mediaid)), file = sys.stderr)
return
return b64encode(mediaid.to_bytes(9, 'big'), b'-_').decode().replace('A', ' ').lstrip().replace(' ','A')
class Instaloader:
def __init__(self,
sleep: bool = True, quiet: bool = False, shorter_output: bool = False, profile_subdirs: bool = True):