Refactor parse_time into its own utils file.
This commit is contained in:
parent
b00ad4ad79
commit
b4e6591c9d
@ -24,18 +24,20 @@ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|||||||
DEALINGS IN THE SOFTWARE.
|
DEALINGS IN THE SOFTWARE.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import requests
|
|
||||||
import json, re, time, copy
|
|
||||||
from collections import deque
|
|
||||||
from threading import Timer
|
|
||||||
from ws4py.client.threadedclient import WebSocketClient
|
|
||||||
from sys import platform as sys_platform
|
|
||||||
from . import endpoints
|
from . import endpoints
|
||||||
from .errors import InvalidEventName, InvalidDestination, GatewayNotFound
|
from .errors import InvalidEventName, InvalidDestination, GatewayNotFound
|
||||||
from .user import User
|
from .user import User
|
||||||
from .channel import Channel, PrivateChannel
|
from .channel import Channel, PrivateChannel
|
||||||
from .server import Server, Member, Permissions, Role
|
from .server import Server, Member, Permissions, Role
|
||||||
from .message import Message
|
from .message import Message
|
||||||
|
from .utils import parse_time
|
||||||
|
|
||||||
|
import requests
|
||||||
|
import json, re, time, copy
|
||||||
|
from collections import deque
|
||||||
|
from threading import Timer
|
||||||
|
from ws4py.client.threadedclient import WebSocketClient
|
||||||
|
from sys import platform as sys_platform
|
||||||
|
|
||||||
def _null_event(*args, **kwargs):
|
def _null_event(*args, **kwargs):
|
||||||
pass
|
pass
|
||||||
@ -238,7 +240,7 @@ class Client(object):
|
|||||||
continue
|
continue
|
||||||
value = data[attr]
|
value = data[attr]
|
||||||
if 'time' in attr:
|
if 'time' in attr:
|
||||||
setattr(message, attr, message._parse_time(value))
|
setattr(message, attr, parse_time(value))
|
||||||
else:
|
else:
|
||||||
setattr(message, attr, value)
|
setattr(message, attr, value)
|
||||||
self._invoke_event('on_message_edit', older_message, message)
|
self._invoke_event('on_message_edit', older_message, message)
|
||||||
|
@ -24,8 +24,7 @@ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|||||||
DEALINGS IN THE SOFTWARE.
|
DEALINGS IN THE SOFTWARE.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import datetime
|
from .utils import parse_time
|
||||||
import re
|
|
||||||
from .user import User
|
from .user import User
|
||||||
|
|
||||||
class Message(object):
|
class Message(object):
|
||||||
@ -75,11 +74,8 @@ class Message(object):
|
|||||||
# we can use this to our advantage to use strptime instead of a complicated parsing routine.
|
# we can use this to our advantage to use strptime instead of a complicated parsing routine.
|
||||||
# example timestamp: 2015-08-21T12:03:45.782000+00:00
|
# example timestamp: 2015-08-21T12:03:45.782000+00:00
|
||||||
# sometimes the .%f modifier is missing
|
# sometimes the .%f modifier is missing
|
||||||
self.edited_timestamp = kwargs.get('edited_timestamp')
|
self.edited_timestamp = parse_time(kwargs.get('edited_timestamp'))
|
||||||
if self.edited_timestamp is not None:
|
self.timestamp = parse_time(kwargs.get('timestamp'))
|
||||||
self.edited_timestamp = self._parse_time(edited_timestamp)
|
|
||||||
|
|
||||||
self.timestamp = self._parse_time(kwargs.get('timestamp'))
|
|
||||||
self.tts = kwargs.get('tts')
|
self.tts = kwargs.get('tts')
|
||||||
self.content = kwargs.get('content')
|
self.content = kwargs.get('content')
|
||||||
self.mention_everyone = kwargs.get('mention_everyone')
|
self.mention_everyone = kwargs.get('mention_everyone')
|
||||||
@ -90,6 +86,3 @@ class Message(object):
|
|||||||
self.mentions = [User(**mention) for mention in kwargs.get('mentions', {})]
|
self.mentions = [User(**mention) for mention in kwargs.get('mentions', {})]
|
||||||
self.attachments = kwargs.get('attachments')
|
self.attachments = kwargs.get('attachments')
|
||||||
|
|
||||||
def _parse_time(self, time_string):
|
|
||||||
return datetime.datetime(*map(int, re.split(r'[^\d]', time_string.replace('+00:00', ''))))
|
|
||||||
|
|
||||||
|
@ -26,7 +26,7 @@ DEALINGS IN THE SOFTWARE.
|
|||||||
|
|
||||||
from .user import User
|
from .user import User
|
||||||
from .permissions import Permissions
|
from .permissions import Permissions
|
||||||
import datetime, re
|
from .utils import parse_time
|
||||||
|
|
||||||
class Role(object):
|
class Role(object):
|
||||||
"""Represents a Discord role in a :class:`Server`.
|
"""Represents a Discord role in a :class:`Server`.
|
||||||
@ -85,7 +85,7 @@ class Member(User):
|
|||||||
super(Member, self).__init__(**user)
|
super(Member, self).__init__(**user)
|
||||||
self.deaf = deaf
|
self.deaf = deaf
|
||||||
self.mute = mute
|
self.mute = mute
|
||||||
self.joined_at = datetime.datetime(*map(int, re.split(r'[^\d]', joined_at.replace('+00:00', ''))))
|
self.joined_at = parse_time(joined_at)
|
||||||
self.roles = roles
|
self.roles = roles
|
||||||
self.status = 'offline'
|
self.status = 'offline'
|
||||||
self.game_id = kwargs.get('game_id', None)
|
self.game_id = kwargs.get('game_id', None)
|
||||||
|
34
discord/utils.py
Normal file
34
discord/utils.py
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
"""
|
||||||
|
The MIT License (MIT)
|
||||||
|
|
||||||
|
Copyright (c) 2015 Rapptz
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a
|
||||||
|
copy of this software and associated documentation files (the "Software"),
|
||||||
|
to deal in the Software without restriction, including without limitation
|
||||||
|
the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||||
|
and/or sell copies of the Software, and to permit persons to whom the
|
||||||
|
Software is furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in
|
||||||
|
all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||||||
|
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||||
|
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||||
|
DEALINGS IN THE SOFTWARE.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from re import split as re_split
|
||||||
|
import datetime
|
||||||
|
|
||||||
|
|
||||||
|
def parse_time(timestamp):
|
||||||
|
if timestamp:
|
||||||
|
return datetime.datetime(*map(int, re_split(r'[^\d]', timestamp.replace('+00:00', ''))))
|
||||||
|
return None
|
Loading…
x
Reference in New Issue
Block a user