Ejemplo n.º 1
0
    def next(self, full=False):
        if self.max_items is not None:
            if self.count >= self.max_items:
                raise StopIteration
        try:
            if pythonver >= 3:
                item = next(self._iter)
            else:
                item = self._iter.next()

            self.count += 1
            if 'timestamp' in item:
                item['timestamp'] = client.parse_timestamp(item['timestamp'])
            if full:
                return item

            if type(self.return_values) is tuple:
                return tuple((item[i] for i in self.return_values))
            elif self.return_values is None:
                return item
            else:
                return item[self.return_values]

        except StopIteration:
            if self.last:
                raise StopIteration
            self.load_chunk()
            return List.next(self, full=full)
Ejemplo n.º 2
0
    def next(self, full=False):
        if self.max_items is not None:
            if self.count >= self.max_items:
                raise StopIteration
        try:
            if pythonver >= 3:
                item = next(self._iter)
            else:
                item = self._iter.next()

            self.count += 1
            if 'timestamp' in item:
                item['timestamp'] = client.parse_timestamp(item['timestamp'])
            if full:
                return item

            if type(self.return_values) is tuple:
                return tuple((item[i] for i in self.return_values))
            elif self.return_values is None:
                return item
            else:
                return item[self.return_values]

        except StopIteration:
            if self.last:
                raise StopIteration
            self.load_chunk()
            return List.next(self, full=full)
Ejemplo n.º 3
0
	def __init__(self, site, name, info = None, extra_properties = {}):
		if type(name) is type(self):
			return self.__dict__.update(name.__dict__)
		self.site = site
		self.name = name
		
		if not info:
			if extra_properties:
				prop = 'info|' + '|'.join(extra_properties.iterkeys())
				extra_props = []
				[extra_props.extend(extra_prop) for extra_prop in extra_properties.itervalues()]					
			else:
				prop = 'info'
				extra_props = ()
			
			info = self.site.api('query', prop = prop, titles = name, 
				inprop = 'protection', *extra_props)
			info = info['query']['pages'].itervalues().next()
		self._info = info
				
		self.namespace = info.get('ns', 0)
		self.name = info.get('title', u'')
		if self.namespace:
			self.page_title = self.strip_namespace(self.name)
		else:
			self.page_title = self.name

		self.touched = client.parse_timestamp(info.get('touched', '0000-00-00T00:00:00Z'))
		self.revision = info.get('lastrevid', 0)
		self.exists = 'missing' not in info
		self.length = info.get('length')
		self.protection = dict([(i['type'], (i['level'], i['expiry'])) for i in info.get('protection', ()) if i])
		self.redirect = 'redirect' in info
		
		self.edit_time = None
Ejemplo n.º 4
0
    def save(self, text=u'', summary=u'', minor=False, bot=True, **kwargs):
        if not self.site.logged_in and self.site.force_login:
            # Should we really check for this?
            raise errors.LoginError(self.site)
        if self.site.blocked:
            raise errors.UserBlocked(self.site.blocked)
        if not self.can('edit'):
            raise errors.ProtectedPageError(self)

        if not text:
            text = self.text

        if not self.site.writeapi:
            return OldPage.save(self, text=text, summary=summary, minor=False)

        data = {}
        if minor:
            data['minor'] = '1'
        if not minor:
            data['notminor'] = '1'
        if self.last_rev_time:
            data['basetimestamp'] = time.strftime('%Y%m%d%H%M%S',
                                                  self.last_rev_time)
        if self.edit_time:
            data['starttimestamp'] = time.strftime('%Y%m%d%H%M%S',
                                                   self.edit_time)
        if bot:
            data['bot'] = '1'

        data.update(kwargs)

        def do_edit():
            result = self.site.api('edit',
                                   title=self.name,
                                   text=text,
                                   summary=summary,
                                   token=self.get_token('edit'),
                                   **data)
            if result['edit'].get('result').lower() == 'failure':
                raise errors.EditError(self, result['edit'])
            return result

        try:
            result = do_edit()
        except errors.APIError as e:
            if e.code == 'badtoken':
                # Retry, but only once to avoid an infinite loop
                self.get_token('edit', force=True)
                try:
                    result = do_edit()
                except errors.APIError as e:
                    self.handle_edit_error(e, summary)
            else:
                self.handle_edit_error(e, summary)

        if result['edit'] == 'Success':
            self.last_rev_time = client.parse_timestamp(result['newtimestamp'])
        return result['edit']
Ejemplo n.º 5
0
    def __init__(self, site, name, info=None, extra_properties={}):
        if type(name) is type(self):
            return self.__dict__.update(name.__dict__)
        self.site = site
        self.name = name
        self.section = None

        if not info:
            if extra_properties:
                prop = 'info|' + '|'.join(extra_properties.iterkeys())
                extra_props = []
                [
                    extra_props.extend(extra_prop)
                    for extra_prop in extra_properties.itervalues()
                ]
            else:
                prop = 'info'
                extra_props = ()

            if type(name) is int:
                info = self.site.api('query',
                                     prop=prop,
                                     pageids=name,
                                     inprop='protection',
                                     *extra_props)
            else:
                info = self.site.api('query',
                                     prop=prop,
                                     titles=name,
                                     inprop='protection',
                                     *extra_props)
            info = info['query']['pages'].itervalues().next()
        self._info = info

        self.namespace = info.get('ns', 0)
        self.name = info.get('title', u'')
        if self.namespace:
            self.page_title = self.strip_namespace(self.name)
        else:
            self.page_title = self.name

        self.touched = client.parse_timestamp(
            info.get('touched', '0000-00-00T00:00:00Z'))
        self.revision = info.get('lastrevid', 0)
        self.exists = 'missing' not in info
        self.length = info.get('length')
        self.protection = dict([(i['type'], (i['level'], i['expiry']))
                                for i in info.get('protection', ()) if i])
        self.redirect = 'redirect' in info
        self.pageid = info.get('pageid', None)
        self.contentmodel = info.get('contentmodel', None)
        self.pagelanguage = info.get('pagelanguage', None)
        self.restrictiontypes = info.get('restrictiontypes', None)

        self.last_rev_time = None
        self.edit_time = None
Ejemplo n.º 6
0
    def save(self, text=u'', summary=u'', minor=False, bot=True, **kwargs):
        if not self.site.logged_in and self.site.force_login:
            # Should we really check for this?
            raise errors.LoginError(self.site)
        if self.site.blocked:
            raise errors.UserBlocked(self.site.blocked)
        if not self.can('edit'):
            raise errors.ProtectedPageError(self)

        if not text:
            text = self.text

        if not self.site.writeapi:
            return OldPage.save(self, text=text, summary=summary, minor=False)

        data = {}
        if minor:
            data['minor'] = '1'
        if not minor:
            data['notminor'] = '1'
        if self.last_rev_time:
            data['basetimestamp'] = time.strftime('%Y%m%d%H%M%S', self.last_rev_time)
        if self.edit_time:
            data['starttimestamp'] = time.strftime('%Y%m%d%H%M%S', self.edit_time)
        if bot:
            data['bot'] = '1'

        data.update(kwargs)

        def do_edit():
            result = self.site.api('edit', title=self.name, text=text, summary=summary, token=self.get_token('edit'), **data)
            if result['edit'].get('result').lower() == 'failure':
                raise errors.EditError(self, result['edit'])
            return result
        try:
            result = do_edit()
        except errors.APIError as e:
            if e.code == 'badtoken':
                # Retry, but only once to avoid an infinite loop
                self.get_token('edit', force=True)
                try:
                    result = do_edit()
                except errors.APIError as e:
                    self.handle_edit_error(e, summary)
            else:
                self.handle_edit_error(e, summary)

        if result['edit'] == 'Success':
            self.last_rev_time = client.parse_timestamp(result['newtimestamp'])
        return result['edit']
Ejemplo n.º 7
0
            return result
        try:
            result = do_edit()
        except errors.APIError, e:
            if e.code == 'badtoken':
                # Retry, but only once to avoid an infinite loop
                self.get_token('edit', force=True)
                try:
                    result = do_edit()
                except errors.APIError, e:
                    self.handle_edit_error(e, summary)
            else:
                self.handle_edit_error(e, summary)

        if result['edit'].get('result') == 'Success':
            self.last_rev_time = client.parse_timestamp(result['edit'].get('newtimestamp'))
        return result['edit']

    def handle_edit_error(self, e, summary):
        if e.code == 'editconflict':
            raise errors.EditError(self, summary, e.info)
        elif e.code in ('protectedtitle', 'cantcreate', 'cantcreate-anon', 'noimageredirect-anon',
                        'noimageredirect', 'noedit-anon', 'noedit'):
            raise errors.ProtectedPageError(self, e.code, e.info)
        else:
            raise

    def get_expanded(self):
        self.site.require(1, 12)

        revs = self.revisions(prop='content', limit=1, expandtemplates=True)
Ejemplo n.º 8
0
			return result	
		try:
			result = do_edit()
		except errors.APIError, e:
			if e.code == 'badtoken':
				# Retry, but only once to avoid an infinite loop
				self.get_token('edit', force = True)
				try:
					result = do_edit()
				except errors.APIError, e:
					self.handle_edit_error(e, summary)
			else:
				self.handle_edit_error(e, summary)

		if result['edit'] == 'Success':
			self.last_rev_time = client.parse_timestamp(result['newtimestamp'])
		return result['edit']
	
	def handle_edit_error(self, e,  summary):
		if e.code == 'editconflict':
			raise errors.EditError(self, summary, e.info)
		elif e.code in ('protectedtitle', 'cantcreate', 'cantcreate-anon', 'noimageredirect-anon', 
			    'noimageredirect', 'noedit-anon', 'noedit'):
			raise errors.ProtectedPageError(self, e.code, e.info)
		else:
			raise		

	def get_expanded(self):
		self.site.require(1, 12)
		
		revs = self.revisions(prop = 'content', limit = 1, expandtemplates = True)
Ejemplo n.º 9
0
    def save(self,
             text,
             summary=u'',
             minor=False,
             bot=True,
             section=None,
             **kwargs):
        """
        Update the text of a section or the whole page by performing an edit operation.
        """
        if not self.site.logged_in and self.site.force_login:
            # Should we really check for this?
            raise errors.LoginError(
                self.site,
                'By default, mwclient protects you from accidentally ' +
                'editing without being logged in. If you actually want to edit without '
                'logging in, you can set force_login on the Site object to False.'
            )
        if self.site.blocked:
            raise errors.UserBlocked(self.site.blocked)
        if not self.can('edit'):
            raise errors.ProtectedPageError(self)

        if self.section is not None and section is None:
            warnings.warn(
                'From mwclient version 0.8.0, the `save()` method will no longer '
                +
                'implicitly use the `section` parameter from the last `text()` or '
                +
                '`edit()` call. Please pass the `section` parameter explicitly to '
                + 'the save() method to save only a single section.',
                category=DeprecationWarning,
                stacklevel=2)
            section = self.section

        if not self.site.writeapi:
            raise errors.NoWriteApi(self)

        data = {}
        if minor:
            data['minor'] = '1'
        if not minor:
            data['notminor'] = '1'
        if self.last_rev_time:
            data['basetimestamp'] = time.strftime('%Y%m%d%H%M%S',
                                                  self.last_rev_time)
        if self.edit_time:
            data['starttimestamp'] = time.strftime('%Y%m%d%H%M%S',
                                                   self.edit_time)
        if bot:
            data['bot'] = '1'
        if section:
            data['section'] = section

        data.update(kwargs)

        def do_edit():
            result = self.site.api('edit',
                                   title=self.name,
                                   text=text,
                                   summary=summary,
                                   token=self.get_token('edit'),
                                   **data)
            if result['edit'].get('result').lower() == 'failure':
                raise errors.EditError(self, result['edit'])
            return result

        try:
            result = do_edit()
        except errors.APIError as e:
            if e.code == 'badtoken':
                # Retry, but only once to avoid an infinite loop
                self.get_token('edit', force=True)
                try:
                    result = do_edit()
                except errors.APIError as e:
                    self.handle_edit_error(e, summary)
            else:
                self.handle_edit_error(e, summary)

        # 'newtimestamp' is not included if no change was made
        if 'newtimestamp' in result['edit'].keys():
            self.last_rev_time = client.parse_timestamp(
                result['edit'].get('newtimestamp'))
        return result['edit']
Ejemplo n.º 10
0
        try:
            result = do_edit()
        except errors.APIError, e:
            if e.code == 'badtoken':
                # Retry, but only once to avoid an infinite loop
                self.get_token('edit', force=True)
                try:
                    result = do_edit()
                except errors.APIError, e:
                    self.handle_edit_error(e, summary)
            else:
                self.handle_edit_error(e, summary)

        if result['edit'] == 'Success':
            self.last_rev_time = client.parse_timestamp(result['newtimestamp'])
        return result['edit']

    def handle_edit_error(self, e, summary):
        if e.code == 'editconflict':
            raise errors.EditError(self, summary, e.info)
        elif e.code in ('protectedtitle', 'cantcreate', 'cantcreate-anon',
                        'noimageredirect-anon', 'noimageredirect',
                        'noedit-anon', 'noedit'):
            raise errors.ProtectedPageError(self, e.code, e.info)
        else:
            raise

    def get_expanded(self):
        self.site.require(1, 12)
Ejemplo n.º 11
0
    def save(self, text, summary=u'', minor=False, bot=True, section=None, **kwargs):
        """
        Update the text of a section or the whole page by performing an edit operation.
        """
        if not self.site.logged_in and self.site.force_login:
            # Should we really check for this?
            raise errors.LoginError(self.site, 'By default, mwclient protects you from accidentally ' +
                                    'editing without being logged in. If you actually want to edit without '
                                    'logging in, you can set force_login on the Site object to False.')
        if self.site.blocked:
            raise errors.UserBlocked(self.site.blocked)
        if not self.can('edit'):
            raise errors.ProtectedPageError(self)

        if self.section is not None and section is None:
            warnings.warn('From mwclient version 0.8.0, the `save()` method will no longer ' +
                          'implicitly use the `section` parameter from the last `text()` or ' +
                          '`edit()` call. Please pass the `section` parameter explicitly to ' +
                          'the save() method to save only a single section.',
                          category=DeprecationWarning, stacklevel=2)
            section = self.section

        if not self.site.writeapi:
            raise errors.NoWriteApi(self)

        data = {}
        if minor:
            data['minor'] = '1'
        if not minor:
            data['notminor'] = '1'
        if self.last_rev_time:
            data['basetimestamp'] = time.strftime('%Y%m%d%H%M%S', self.last_rev_time)
        if self.edit_time:
            data['starttimestamp'] = time.strftime('%Y%m%d%H%M%S', self.edit_time)
        if bot:
            data['bot'] = '1'
        if section:
            data['section'] = section

        data.update(kwargs)

        def do_edit():
            result = self.site.api('edit', title=self.name, text=text,
                                   summary=summary, token=self.get_token('edit'),
                                   **data)
            if result['edit'].get('result').lower() == 'failure':
                raise errors.EditError(self, result['edit'])
            return result
        try:
            result = do_edit()
        except errors.APIError as e:
            if e.code == 'badtoken':
                # Retry, but only once to avoid an infinite loop
                self.get_token('edit', force=True)
                try:
                    result = do_edit()
                except errors.APIError as e:
                    self.handle_edit_error(e, summary)
            else:
                self.handle_edit_error(e, summary)

        # 'newtimestamp' is not included if no change was made
        if 'newtimestamp' in result['edit'].keys():
            self.last_rev_time = client.parse_timestamp(result['edit'].get('newtimestamp'))
        return result['edit']