Profile does not exist. Instaloader

204 views Asked by At

I am trying to do scraping with the instaloader library in python in a firebase cloud function, but I have a strange behavior.

When I run my code in the local firebase emulator it works perfect and I save the account trackers, but when the function runs in production I get the following in console:

Error: Profile A does not exist.

The most similar profiles are: A, B, C, D, E.

Instaloader Version: instaloader==4.8.1

My code is as follows:

import random
import logging
import json
import instaloader
from firebase_init import db
from firebase_admin import firestore

logging.basicConfig(level=logging.INFO)

def get_profile_followers():
    try:
        profile_name = random.choice(['A', 'B', 'C'])
        logging.info(f'Profile name selected: {profile_name}')

        # Obtener imágenes existentes
        doc_ref = db.collection('profiles').document('test')
        doc = doc_ref.get()
        existing_followers = doc.to_dict().get('followers', []) if doc.exists else []
        limit_value = doc.to_dict().get('limitFollowers', 100) if doc.exists else 100
        existing_followers_usernames = {follower['username'] for follower in existing_followers}
        followers_count = len(existing_followers)
        
        if followers_count >= 1300:
            logging.info(f'Limite de seguidores alcanzado: {followers_count}')
            return f'Limite de seguidores alcanzado: {followers_count}'
        else:
            # Se obtienen seguidores de la cuenta
            L = instaloader.Instaloader()
            L.load_session_from_file('account', filename=f'./mysessionfile')
            
            profile_followers = instaloader.Profile.from_username(L.context, profile_name).get_followers()
            
            followers = []
            
            count = 0
            for follower in profile_followers:
                if count >= limit_value:
                    break
                
                if follower.username in existing_followers_usernames:
                    logging.info(f'Follower {follower.username} ya existe')
                    logging.info('=================')
                else:
                    follower_info = {
                        'username': follower.username,
                        'isPrivate': follower.is_private,
                    }
                    logging.info(f'Perfil Numero {count + 1}: {follower_info} guardado')
                    followers.append(follower_info)
                    count += 1
                
            doc_ref = db.collection('profiles').document('test')
            batch = db.batch()
            batch.update(doc_ref, {'followers': firestore.ArrayUnion(followers)})
            batch.commit()
            
            
            response_data = {
                'message': f'Seguidores de {profile_name} guardados',
                'Total': len(followers),
            }
            response_json = json.dumps(response_data)

            return f'Completed: {response_json}'
    except Exception as e:
        logging.error(f'Error: {e}')
        return f'Error: {e}'

I was hoping it would start tracking each follower as it behaves in the local emulator:

i  functions: Beginning execution of "us-central1-profile_scrap"
>  INFO:root:Profile name selected: 'profile_scraped'
>  Loaded session from ./mysessionfile.
>  INFO:root:Perfil Numero 1: {'username': 'A', 'isPrivate': True} guardado
>  INFO:root:Perfil Numero 2: {'username': 'B', 'isPrivate': False} guardado
>  INFO:root:Perfil Numero 3: {'username': 'C', 'isPrivate': True} guardado
>  INFO:root:Perfil Numero 4: {'username': 'D', 'isPrivate': True} guardado
.
.
.
1

There are 1 answers

0
Greg Fenton On

I'm not 100% positive but I suspect the issue is that you are trying to write a file to the current directory (./mysessionfile) but in the cloud instance you are limited as to where your code can write files. See this section of the docs. Most places I've seen talking about writing files in Cloud Functions recommend writing to /tmp.