def test_instantiate_from_instance(self):
     """Test passing instance to factory methods works."""
     t1 = T.utcnow()
     self.assertIsNot(t1, T.fromISOformat(t1))
     self.assertEqual(t1, T.fromISOformat(t1))
     self.assertIsInstance(T.fromISOformat(t1), T)
     self.assertIsNot(t1, T.fromtimestampformat(t1))
     self.assertEqual(t1, T.fromtimestampformat(t1))
     self.assertIsInstance(T.fromtimestampformat(t1), T)
 def test_instantiate_from_instance(self):
     """Test passing instance to factory methods works."""
     t1 = Timestamp.utcnow()
     self.assertIsNot(t1, Timestamp.fromISOformat(t1))
     self.assertEqual(t1, Timestamp.fromISOformat(t1))
     self.assertIsInstance(Timestamp.fromISOformat(t1), Timestamp)
     self.assertIsNot(t1, Timestamp.fromtimestampformat(t1))
     self.assertEqual(t1, Timestamp.fromtimestampformat(t1))
     self.assertIsInstance(Timestamp.fromtimestampformat(t1), Timestamp)
Beispiel #3
0
 def test_short_mediawiki_format(self):
     """Test short mw timestamp conversion from and to Timestamp format."""
     t1 = Timestamp(2018, 12, 17)
     t2 = Timestamp.fromtimestampformat('20181217')  # short timestamp
     ts1 = t1.totimestampformat()
     ts2 = t2.totimestampformat()
     self.assertEqual(t1, t2)
     self.assertEqual(ts1, ts2)
 def test_mediawiki_format(self):
     t1 = T.utcnow()
     ts1 = t1.totimestampformat()
     t2 = T.fromtimestampformat(ts1)
     ts2 = t2.totimestampformat()
     # MediaWiki timestamp format doesn't include microseconds
     self.assertNotEqual(t1, t2)
     t1 = t1.replace(microsecond=0)
     self.assertEqual(t1, t2)
     self.assertEqual(ts1, ts2)
 def test_mediawiki_format(self):
     t1 = T.utcnow()
     ts1 = t1.totimestampformat()
     t2 = T.fromtimestampformat(ts1)
     ts2 = t2.totimestampformat()
     # MediaWiki timestamp format doesn't include microseconds
     self.assertNotEqual(t1, t2)
     t1 = t1.replace(microsecond=0)
     self.assertEqual(t1, t2)
     self.assertEqual(ts1, ts2)
Beispiel #6
0
 def test_two_revisions_reverse_order(self):
     revisions_wrong_order = [
         Revision(
             2,
             Timestamp.fromtimestampformat('20200115'),
             'test_user',
             slots={
                 'main': {
                     'contentmodel':
                     'wikitext',
                     'contentformat':
                     'text/x-wiki',
                     '*':
                     '[[Изображение:test.gif|thumb]]\n\ntest test test\n\nnewline\n'
                 }
             }),
         Revision(
             1,
             Timestamp.fromtimestampformat('20200112'),
             'test_user',
             slots={
                 'main': {
                     'contentmodel':
                     'wikitext',
                     'contentformat':
                     'text/x-wiki',
                     '*':
                     '[[Изображение:test.gif|thumb]]\n\nold\n\ntest test test\n\n'
                 }
             })
     ]
     diffs = ApiWrapper._convert_revisions_to_diffs(revisions_wrong_order)
     self.assertEqual(1, len(diffs))
     self.assertEqual(
         RevisionDiff(old_ts=datetime.strptime('2020-01-12T00:00:00Z',
                                               '%Y-%m-%dT%H:%M:%SZ'),
                      new_ts=datetime.strptime('2020-01-15T00:00:00Z',
                                               '%Y-%m-%dT%H:%M:%SZ'),
                      old_revid=1,
                      new_revid=2,
                      lines_diffs=[(None, 'old\n'), (None, 'newline\n')]),
         diffs[0])
Beispiel #7
0
 def test_mediawiki_format(self):
     """Test conversion from and to Timestamp format."""
     t1 = Timestamp.utcnow()
     if not t1.microsecond:  # T191827: ensure microsecond is not 0
         t1 = t1.replace(microsecond=1000)
     ts1 = t1.totimestampformat()
     t2 = Timestamp.fromtimestampformat(ts1)
     ts2 = t2.totimestampformat()
     # MediaWiki timestamp format doesn't include microseconds
     self.assertNotEqual(t1, t2)
     t1 = t1.replace(microsecond=0)
     self.assertEqual(t1, t2)
     self.assertEqual(ts1, ts2)
Beispiel #8
0
    def __init__(self, **kwargs):
        """Initializer.

        @keyword site: a project site object. Used when no url is given
        @type site: APISite
        @keyword since: a timestamp for older events; there will likely be
            between 7 and 31 days of history available but is not guaranteed.
            It may be given as a pywikibot.Timestamp, an ISO 8601 string
            or a mediawiki timestamp string.
        @type since: pywikibot.Timestamp or str
        @keyword streams: event stream types. Mandatory when no url is given.
            Multiple streams may be given as a string with comma separated
            stream types or an iterable of strings
            Refer https://stream.wikimedia.org/?doc for available
            wikimedia stream types.
        @type streams: str or iterable
        @keyword timeout: a timeout value indication how long to wait to send
            data before giving up
        @type timeout: int, float or a tuple of two values of int or float
        @keyword url: an url retrieving events from. Will be set up to a
            default url using _site.family settings, stream types and timestamp
        @type url: str
        @param kwargs: keyword arguments passed to SSEClient and requests lib
        @raises ImportError: sseclient is not installed
        @raises NotImplementedError: no stream types specified
        """
        if isinstance(EventSource, Exception):
            raise ImportError('sseclient is required for EventStreams;\n'
                              'install it with "pip install sseclient"\n')
        self.filter = {'all': [], 'any': [], 'none': []}
        self._total = None
        self._site = kwargs.pop('site', Site())

        self._streams = kwargs.pop('streams', None)
        if self._streams and not isinstance(self._streams, StringTypes):
            self._streams = ','.join(self._streams)

        self._since = kwargs.pop('since', None)
        if self._since:
            # assume this is a mw timestamp, convert it to a Timestamp object
            if isinstance(self._streams, StringTypes) \
               and '-' not in self._since:
                self._since = Timestamp.fromtimestampformat(self._since)
            if isinstance(self._streams, Timestamp):
                self._since = self._since.isoformat

        self._url = kwargs.get('url') or self.url
        kwargs.setdefault('url', self._url)
        kwargs.setdefault('timeout', config.socket_timeout)
        self.sse_kwargs = kwargs
Beispiel #9
0
    def __init__(self, **kwargs):
        """Initializer.

        @keyword site: a project site object. Used when no url is given
        @type site: APISite
        @keyword since: a timestamp for older events; there will likely be
            between 7 and 31 days of history available but is not guaranteed.
            It may be given as a pywikibot.Timestamp, an ISO 8601 string
            or a mediawiki timestamp string.
        @type since: pywikibot.Timestamp or str
        @keyword streams: event stream types. Mandatory when no url is given.
            Multiple streams may be given as a string with comma separated
            stream types or an iterable of strings
            Refer https://stream.wikimedia.org/?doc for available
            wikimedia stream types.
        @type streams: str or iterable
        @keyword timeout: a timeout value indication how long to wait to send
            data before giving up
        @type timeout: int, float or a tuple of two values of int or float
        @keyword url: an url retrieving events from. Will be set up to a
            default url using _site.family settings, stream types and timestamp
        @type url: str
        @param kwargs: keyword arguments passed to SSEClient and requests lib
        @raises ImportError: sseclient is not installed
        @raises NotImplementedError: no stream types specified
        """
        if isinstance(EventSource, Exception):
            raise ImportError('sseclient is required for EventStreams;\n'
                              'install it with "pip install sseclient"\n')
        self.filter = {'all': [], 'any': [], 'none': []}
        self._total = None
        self._site = kwargs.pop('site', Site())

        self._streams = kwargs.pop('streams', None)
        if self._streams and not isinstance(self._streams, StringTypes):
            self._streams = ','.join(self._streams)

        self._since = kwargs.pop('since', None)
        if self._since:
            # assume this is a mw timestamp, convert it to a Timestamp object
            if isinstance(self._streams, StringTypes) \
               and '-' not in self._since:
                self._since = Timestamp.fromtimestampformat(self._since)
            if isinstance(self._streams, Timestamp):
                self._since = self._since.isoformat

        self._url = kwargs.get('url') or self.url
        kwargs.setdefault('url', self._url)
        kwargs.setdefault('timeout', config.socket_timeout)
        self.sse_kwargs = kwargs
def active_and_future_campaigns():
    from pywikibot.data.api import Request
    from pywikibot import Timestamp

    parameters = {
        'action': 'query',
        'list': 'centralnoticeactivecampaigns',
        'cnacincludefuture': ''
    }

    request = Request(_site, parameters=parameters)

    # TODO Error handling
    raw_query_data = request.submit()
    raw_campaigns = (
        raw_query_data['query']['centralnoticeactivecampaigns']['campaigns'])

    # Convert start and end to datetime objects
    for c in raw_campaigns:
        c['start'] = Timestamp.fromtimestampformat(c['start'])
        c['end'] = Timestamp.fromtimestampformat(c['end'])

    return raw_campaigns