Arkadaşlar öncelikle gerekli kütüphaneleri yüklüyoruz re, ve requests kütüphaneleri yüklü olması gerekmektedir daha sonra kodumuzu çaliştirdiktan sonra 2 parametre ekledim login yazıp enterliyoruz kullanıcı bilgilerimizile 1 kereligine giriş yapiyoruz ardından hesabın çerez bilgileri kodun olan kısma cookies.json olarak kaydediyor daha sonra like yaziyoruz beğeni işlemine devam ediyor bu defa kaydedtiimiz çerez bilgleieryle devam edecektirç artık tekrrdan giriş yapmanıza gerek yok ardından sizden post linki iseryecektir post linki yapişirip enterliyoruz giriş yaptıgımız hesap ile beğeni işlemi yapiyor !

import json
import requests
import re

def login(username , password):
    headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36',
    'Accept': '*/*',
    'Accept-Language': 'tr-TR,tr;q=0.8,en-US;q=0.5,en;q=0.3',
    'Accept-Encoding': 'gzip, deflate, br',
    'X-CSRFToken': csrftokens,
    'X-Instagram-AJAX': '',
    'X-IG-App-ID': '',
    'X-ASBD-ID': '',
    'X-IG-WWW-Claim': '',
    'Content-Type': 'application/x-www-form-urlencoded',
    'X-Requested-With': 'XMLHttpRequest',
    'Origin': 'https://www.instagram.com',
    'DNT': '1',
    'Connection': 'keep-alive',
    'Referer': 'https://www.instagram.com/accounts/login/',
    'Sec-Fetch-Dest': 'empty',
    'Sec-Fetch-Mode': 'cors',
    'Sec-Fetch-Site': 'same-origin',
    }
    data = {
        'enc_password': f'#PWD_INSTAGRAM_BROWSER:0:0:{password}',
        'optIntoOneTap': 'true',
        'queryParams': '{}',
        'trustedDeviceRecords': '{}',
        f'username': {username},
    }
 
    # Create a session object to manage the session and cookies
    session = requests.Session()
    
    # Make the POST request using the session object
    response = session.post('https://www.instagram.com/api/v1/web/accounts/login/ajax/', headers=headers, data=data)
    # Get the JSON response from the server
    girilen = response.json()
    # Print the response
    print(girilen)
    # Save the cookies to a 'cookies.json' file
    with open('cookies.json', 'w') as cookies_file:
        json.dump(session.cookies.get_dict(), cookies_file)
    print(username , "Çerezleri Cookies.json olarak kaydedildi artık  giriş yapmanıza gerek yok")


# Read cookies from 'cookies.json'
with open('cookies.json', 'r') as cookies_file:
    cookies_data = json.load(cookies_file)
csrftokens = cookies_data['csrftoken']
# Define the headers and data for the POST request (same as before)
headers = {
    'X-IG-WWW-Claim': 'hmac.AR3XNgbRDJBTWuLwcUef6FmL0redHplVN_KEInpPJEWWiKxO',
    'X-Instagram-AJAX': '1007967966',
    'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1',
    'viewport-width': '390',
    'Content-Type': 'application/x-www-form-urlencoded',
    'Accept': '*/*',
    'X-Requested-With': 'XMLHttpRequest',
    'X-ASBD-ID': '129477',
    'X-CSRFToken': csrftokens,
    'sec-ch-prefers-color-scheme': 'light',
    'X-IG-App-ID': '936619743392459',
}

def get_media_id_from_url(url):
    def extract_short_code(url):
        pattern = r'/p/([A-Za-z0-9_-]+)/'
        matches = re.findall(pattern, url)
        if matches:
            return matches[0]
        else:
            return None
    def code_to_media_id(short_code):
        alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_'
        media_id = 0
        for letter in short_code:
            media_id = (media_id * 64) + alphabet.index(letter)
        return media_id
    short_code = extract_short_code(url)
    if short_code:
        media_id = code_to_media_id(short_code)
        return media_id
    else:
        return None
def begeni_yap(post_link):
    post_id = get_media_id_from_url(post_link)
    if not post_id:
        print("Invalid post link. Please provide a valid Instagram post link.")
        return
    
    # Update the URL with the extracted post_id
    url = f'https://www.instagram.com/api/v1/web/likes/{post_id}/like/'
    
    # Make the POST request using the session object
    response = requests.post(url, headers=headers, cookies=cookies_data)
    # Get the JSON response from the server
    girilen = response.json()
    if girilen['status'] == "ok":
        print('Beğeni Yapıldı')
    else:
        print('Beğeni Yapılamadı sorun oluştu.')
    # Print the response
    print(girilen)
# Get the post link from the user as input

# Continue with other parts of the script as needed (e.g., extracting and printing CSRF token, mid, etc.)


def main():
    while True:
        komut = input("Komutunuzu girin (Giriş yapmak için login yazin beğeni işlemi için like yazın ): ")
        if komut == "login":
            kullaniciadi = input("Kullanıcı adınızı Giriniz : ")
            sifre = input("Şifrenizi Giriniz : ")
            login(kullaniciadi , sifre)
        elif komut == "like":
            post = input("post linki: ")
            begeni_yap(post)
        else:
            print("Geçersiz komut! Tekrar deneyin.")
if __name__ == "__main__":
    main()