Example #1
0
def announce(text, token, room=None):
    from matrix_client.api import MatrixHttpApi
    matrix = MatrixHttpApi("https://matrix.org", token=token)
    matrix.sync()
    roomid = matrix.get_room_id(room)
    matrix.join_room(roomid)
    matrix.send_message(roomid, text)
Example #2
0
class CrawlerMatrixClient:
    def __init__(self, token, server, roomid):
        self.server = server
        self.roomid = roomid
        self.token = token
        self.APIWrapper = MatrixHttpApi("https://{}".format(self.server), token=self.token)
        self.next_batch = self.APIWrapper.sync().get("next_batch")
        print("client initialized")

    def getChunk(self):
        b = self.APIWrapper.get_room_messages(self.roomid, self.next_batch, "b", limit=100)
        c = b.get("chunk")
        self.next_batch = b.get("end")
        return c

    def try_recover_suspend(self, output_file, contents, timestamps, names, message_count):
        with open(output_file, "r") as handle:
            a = handle.readlines()
            if a[len(a)-1] == "sus":
                print("\n\nrestoring from suspend'\n\n")
                self.next_batch = a[len(a)-2]
                a = a[:-2]
                for element in a:
                    b = element.split(";")
                    timestamps.append(b[0])
                    names.append(b[1])
                    contents.append(b[2])
                print("restored")
                return message_count - len(a) + 1 

    def suspend(self, contents, timestamps, names, output_file):
        print("suspending to output_file")
        with open(output_file, "w+") as handle:
            for i in range(len(contents)-1):
                handle.write(str(timestamps[i]))
                handle.write(";")
                handle.write(str(names[i]))
                handle.write(";")
                content = base64.b64encode(str.encode(str(contents[i]))).decode()
                handle.write(content)
                handle.write("\n")
            handle.write(self.next_batch)
            handle.write("\n")
            handle.write("sus")

    def dump_message_events(self, message_count, output_file):
        contents = []
        timestamps = []
        names = []
        try:
            message_count = self.try_recover_suspend(output_file, contents, timestamps, names, message_count)
        except:
            pass
        try:
            count = message_count // 100
            for progress in range(count):
                chunk = self.getChunk()
                for element in chunk:
                    content = element.get("content").get("body")
                    if content is not None:
                        timestamps.append(element.get("origin_server_ts"))
                        contents.append(content)
                        names.append(element.get("sender"))
                print("haha progress bar go brr {} out of {}".format(progress, count), end='\r')
            with open(output_file, "w+") as handle:
                for i in range(len(contents)-1):
                    handle.write(str(timestamps[i]))
                    handle.write(";")
                    handle.write(str(names[i]))
                    handle.write(";")
                    content = base64.b64encode(str.encode(str(contents[i]))).decode()
                    handle.write(content)
                    handle.write("\n")
                handle.write(self.next_batch)
                handle.write("\n")
                handle.write("sus")
        except KeyboardInterrupt:
            self.suspend(contents, timestamps, names, output_file)
def chat(request, update=""):
    user_name = None
    storage = get_messages(request)
    for message in storage:
        user_name = message
        print("MESSAGE : ", message)

    if user_name != None:
        print("username: "******"LOGIN VARS")
        print("session.matrix_user_name ", session.matrix_user_name)
        print("session.matrix_room_name ", session.matrix_room_name)
        print("session.matrix_server ", session.matrix_server)
        print("session.message_count ", session.message_count)
        print("session.show_images ", session.show_images)
        sys.stdout.flush()
        api = MatrixHttpApi(session.matrix_server, token=session.matrix_token)
        print("GET_MEMBERSHIP")
        api.join_room(api.get_room_id(session.matrix_room_name))
    else:
        return HttpResponseRedirect('/')

    if request.method == 'POST':  #If the user hit send button
        try:
            print("Posting chat")
            sys.stdout.flush()
            chat_form = ChatForm(request.POST)
            if chat_form.is_valid():
                response = api.send_message(
                    api.get_room_id(session.matrix_room_name),
                    chat_form.cleaned_data['text_entered'])
                chat_form = ChatForm()
                room_topic = api.get_room_topic(
                    api.get_room_id(session.matrix_room_name))['topic']
                messages.add_message(request, messages.INFO,
                                     session.matrix_user_name)
                synced = _get_messages(request,
                                       sync_token="end",
                                       direction='f')
                session.messages = json.dumps(synced['chunk'] +
                                              jsonDec.decode(session.messages))
                session.save()
                return render(
                    request, 'client_app/chat.html', {
                        'chat_form': chat_form,
                        'name': session.matrix_user_name,
                        'messages': jsonDec.decode(session.messages),
                        'room': session.matrix_room_name,
                        'topic': room_topic,
                        'show_images': session.show_images
                    })

        except MatrixRequestError as e:
            print(str(e))
            sys.stdout.flush()
            form = NameForm(request.POST)
            return render(request, 'client_app/login.html', {
                'form': form,
                'login_error': True,
                'error_text': str(e)
            })
        else:
            return render(
                request, 'client_app/chat.html', {
                    'chat_form': chat_form,
                    'name': session.matrix_user_name,
                    'messages': jsonDec.decode(session.messages),
                    'room': session.matrix_room_name,
                    'topic': room_topic,
                    'show_images': session.show_images
                })
    if update == "":  #If not asking for an update, get first sync to server
        try:
            chat_form = ChatForm()
            synced = api.sync()
            room_topic = api.get_room_topic(
                api.get_room_id(session.matrix_room_name))['topic']
            session.matrix_sync_token = synced["next_batch"]
            messages.add_message(request, messages.INFO,
                                 session.matrix_user_name)
            synced = _get_messages(request, sync_token="start", direction='b')
            session.messages = json.dumps(synced['chunk'])
            session.save()

        except MatrixRequestError as e:
            print(str(e))
            sys.stdout.flush()
            form = NameForm(request.POST)
            return render(request, 'client_app/login.html', {
                'form': form,
                'login_error': True
            })
        else:
            return render(
                request, 'client_app/chat.html', {
                    'chat_form': chat_form,
                    'name': session.matrix_user_name,
                    'messages': jsonDec.decode(session.messages),
                    'room': session.matrix_room_name,
                    'topic': room_topic,
                    'show_images': session.show_images
                })
    else:  # update is requested so return next messages using sync token from initial sync
        chat_form = ChatForm()
        room_topic = api.get_room_topic(
            api.get_room_id(session.matrix_room_name))['topic']
        messages.add_message(request, messages.INFO, session.matrix_user_name)
        synced = _get_messages(request, sync_token="end", direction='f')
        session.messages = json.dumps(synced['chunk'] +
                                      jsonDec.decode(session.messages))
        session.save()
        return render(
            request, 'client_app/chat.html', {
                'chat_form': chat_form,
                'name': session.matrix_user_name,
                'messages': jsonDec.decode(session.messages),
                'room': session.matrix_room_name,
                'topic': room_topic,
                'show_images': session.show_images
            })