Untitled

mail@pastecode.io avatar
unknown
python
2 years ago
2.0 kB
3
Indexable
Never

import asyncio
from instagrapi import Client
from fbns_mqtt import fbns_mqtt


STOP = asyncio.Event()

class my_instaClient(Client):

    def register_push(self, token):
        endpoint = "push/register/"
        params = dict(
            device_type="android_mqtt",
            is_main_push_channel=True,
            phone_id=self.phone_id,
            device_token=token,  # fbns_token
            guid=self.uuid,
            users=self.user_id,
        )

        res = self.private_request(endpoint, data=params, with_signature=False)
        return res




class Connection_MQTT(my_instaClient):
    def __init__(self,session):
        super(Connection_MQTT, self).__init__()

        self.login_by_sessionid(session)


    async def connect_mqtt(self):
        self.mqttClient = fbns_mqtt.FBNSMQTTClient()


        self.mqttClient.on_fbns_auth    = self.on_fbns_auth
        self.mqttClient.on_fbns_token   = self.on_fbns_token
        self.mqttClient.on_fbns_message = self.on_fbns_message


        await self.mqttClient.connect("mqtt-mini.facebook.com",443,ssl=True,keepalive=900)
        await STOP.wait()
        await self.mqttClient.disconnect()


    def on_fbns_auth(self, auth):
        print("--------------AUTH--------------")
        print(auth)
        print("--------------------------------")


    def on_fbns_token(self,token):
        print("--------------TOKEN--------------")
        print(token)
        self.register_push(token)
        print("--------------------------------")


    def on_fbns_message(self,push):
        print("--------------PUSH--------------")
        if push.payload:
            print(push.payload)
        print("--------------------------------")



if __name__ == "__main__":
    loop = asyncio.get_event_loop()
    app = Connection_MQTT("8526398899%3AWsU8SXSb2HSK30%3A0%3AAYenuzZ9R6t3IYYOHzr_0aqOiUYI-wGOMwSspq5bIw")
    try:
        loop.run_until_complete(app.connect_mqtt())
    except asyncio.CancelledError:
        pass