Convert durations to timedeltas

On:
- AudioAttachment.duration
- VideoAttachment.duration
- Client.onCallEnded call_duration argument
- Client.muteThread mute_time argument
This commit is contained in:
Mads Marquart
2019-09-08 15:04:53 +02:00
parent 2e53963398
commit 24cf4047b7
3 changed files with 37 additions and 11 deletions

View File

@@ -1,3 +1,4 @@
import datetime
import time
import json
import requests
@@ -2109,15 +2110,19 @@ class Client:
j = self._payload_post("/ajax/mercury/delete_messages.php?dpr=1", data)
return True
def muteThread(self, mute_time=-1, thread_id=None):
def muteThread(self, mute_time=None, thread_id=None):
"""Mute thread.
Args:
mute_time: Mute time in seconds, leave blank to mute forever
mute_time (datetime.timedelta): Time to mute, use ``None`` to mute forever
thread_id: User/Group ID to mute. See :ref:`intro_threads`
"""
thread_id, thread_type = self._getThread(thread_id, None)
data = {"mute_settings": str(mute_time), "thread_fbid": thread_id}
if mute_time is None:
mute_settings = -1
else:
mute_settings = _util.timedelta_to_seconds(mute_time)
data = {"mute_settings": str(mute_settings), "thread_fbid": thread_id}
j = self._payload_post("/ajax/mercury/change_mute_thread.php?dpr=1", data)
def unmuteThread(self, thread_id=None):
@@ -2126,7 +2131,7 @@ class Client:
Args:
thread_id: User/Group ID to unmute. See :ref:`intro_threads`
"""
return self.muteThread(0, thread_id)
return self.muteThread(datetime.timedelta(0), thread_id)
def muteThreadReactions(self, mute=True, thread_id=None):
"""Mute thread reactions.
@@ -2468,7 +2473,9 @@ class Client:
elif delta_type == "rtc_call_log":
thread_id, thread_type = getThreadIdAndThreadType(metadata)
call_status = delta["untypedData"]["event"]
call_duration = int(delta["untypedData"]["call_duration"])
call_duration = _util.seconds_to_timedelta(
int(delta["untypedData"]["call_duration"])
)
is_video_call = bool(int(delta["untypedData"]["is_video_call"]))
if call_status == "call_started":
self.onCallStarted(
@@ -3595,7 +3602,7 @@ class Client:
mid: The action ID
caller_id: The ID of the person who ended the call
is_video_call: True if it was video call
call_duration: Call duration in seconds
call_duration (datetime.timedelta): Call duration
thread_id: Thread ID that the action was sent to. See :ref:`intro_threads`
thread_type (ThreadType): Type of thread that the action was sent to. See :ref:`intro_threads`
ts: A timestamp of the action

View File

@@ -1,4 +1,5 @@
import attr
from . import _util
from ._attachment import Attachment
@@ -36,7 +37,7 @@ class AudioAttachment(Attachment):
filename = attr.ib(None)
#: URL of the audio file
url = attr.ib(None)
#: Duration of the audio clip in milliseconds
#: Duration of the audio clip as a timedelta
duration = attr.ib(None)
#: Audio type
audio_type = attr.ib(None)
@@ -49,7 +50,7 @@ class AudioAttachment(Attachment):
return cls(
filename=data.get("filename"),
url=data.get("playable_url"),
duration=data.get("playable_duration_in_ms"),
duration=_util.millis_to_timedelta(data.get("playable_duration_in_ms")),
audio_type=data.get("audio_type"),
)
@@ -175,7 +176,7 @@ class VideoAttachment(Attachment):
width = attr.ib(None)
#: Height of original video
height = attr.ib(None)
#: Length of video in milliseconds
#: Length of video as a timedelta
duration = attr.ib(None)
#: URL to very compressed preview video
preview_url = attr.ib(None)
@@ -243,7 +244,7 @@ class VideoAttachment(Attachment):
return cls(
width=data.get("original_dimensions", {}).get("width"),
height=data.get("original_dimensions", {}).get("height"),
duration=data.get("playable_duration_in_ms"),
duration=_util.millis_to_timedelta(data.get("playable_duration_in_ms")),
preview_url=data.get("playable_url"),
small_image=data.get("chat_image"),
medium_image=data.get("inbox_image"),
@@ -255,7 +256,7 @@ class VideoAttachment(Attachment):
def _from_subattachment(cls, data):
media = data["media"]
return cls(
duration=media.get("playable_duration_in_ms"),
duration=_util.millis_to_timedelta(media.get("playable_duration_in_ms")),
preview_url=media.get("playable_url"),
medium_image=media.get("image"),
uid=data["target"].get("video_id"),

View File

@@ -269,3 +269,21 @@ def datetime_to_millis(dt):
The returned milliseconds will be rounded to the nearest whole number.
"""
return round(dt.timestamp() * 1000)
def seconds_to_timedelta(seconds):
"""Convert seconds to a timedelta."""
return datetime.timedelta(seconds=seconds)
def millis_to_timedelta(milliseconds):
"""Convert a duration (in milliseconds) to a timedelta object."""
return datetime.timedelta(milliseconds=milliseconds)
def timedelta_to_seconds(td):
"""Convert a timedelta to seconds.
The returned seconds will be rounded to the nearest whole number.
"""
return round(td.total_seconds())