Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
3.6 kB
3
Indexable
Never
    async def getChannelData():
        print("Get Channel Data Function Started")
        chat_id = main_chat_id
        i = starting_id
        token_data = await generate_access_token(client_id, client_secret, refresh_token)
        access_token = token_data.get('access_token')
        # as the value of expires_in is in seconds i.e. 3600 seconds = 1 hour we need to add current time to it to get the expiration time
        expires = token_data.get('expires_in') + datetime.now().timestamp()
        print(expires)
        while i < last_id + 1:
            try:
                if expires < datetime.now().timestamp() + 600: # 10 minutes
                    print("Access token expired or about to expire, refreshing...")
                    token_data = await generate_access_token(client_id, client_secret, refresh_token)
                    access_token = token_data.get('access_token')
                    expires = token_data.get('expires_in') + datetime.now().timestamp()
                print(i)
                # print time remaining for token to expire
                print("Time remaining for token to expire: ", datetime.fromtimestamp(expires) - datetime.now())
                message = await app.get_messages(int(chat_id), i)
                media = get_media_from_message(message)
                if media:
                    file_id = message.id
                    name = media.file_name or 'null'
                    extension = name.split('.')[-1]
                    # if media is audio
                    if hasattr(media, 'performer') and hasattr(media, 'title'):
                        name = f"{media.title} - {media.performer}.{extension}"
                    elif hasattr(media, 'title'):
                        name = f"{media.title}.{extension}"
                    elif hasattr(media, 'performer'):
                        name = f"{media.performer}.{extension}"
                    else:
                        name = name
                    size = media.file_size or '0'
                    file_unique_id = media.file_unique_id or ''
                    try:
                        mime_type = media.mime_type or ''
                    except:
                        mime_type = 'null'
                    date = media.date or ''
                    if name == 'null':
                        i += 1
                        continue
                    if int(size) >= 1000000:
                        message_data = {
                            "_id": 1000000000 + file_id,
                            "name": name,
                            "size": size,
                            "mime_type": mime_type,
                            "date": date,
                            "file_unique_id": file_unique_id,
                            "user_id": "",
                        }
                        message_data['date'] = message_data['date'].strftime("%Y-%m-%d %H:%M:%S")
                        json_data = json.dumps(message_data)
                        print(json_data)
                        create_json_file_on_drive(access_token, shared_drive_id, json_data)
                else:
                    i += 1
                    continue
            except FloodWait as e:
                print("Flood Wait for ", e.value)
                await asyncio.sleep(e.value)  # Wait "value" seconds before continuing
                continue
            except Exception as e:
                print("Caught Error: ", e)
                i += 1
                continue
            i += 1
Leave a Comment