Mercurial > libervia-backend
comparison src/plugins/plugin_blog_import.py @ 2370:2c2b826b0bb3
plugin import: node can now be specified + added a "session" dict to keep import session data:
import session data are data that can be used by importer to store anything which can be useful to keep between import methods.
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 06 Oct 2017 08:52:51 +0200 |
parents | cdaa58e14553 |
children | 66baa687c682 |
comparison
equal
deleted
inserted
replaced
2369:cdaa58e14553 | 2370:2c2b826b0bb3 |
---|---|
25 from twisted.internet import defer | 25 from twisted.internet import defer |
26 from twisted.web import client as web_client | 26 from twisted.web import client as web_client |
27 from twisted.words.xish import domish | 27 from twisted.words.xish import domish |
28 from sat.core import exceptions | 28 from sat.core import exceptions |
29 from sat.tools import xml_tools | 29 from sat.tools import xml_tools |
30 import collections | |
31 import os | 30 import os |
32 import os.path | 31 import os.path |
33 import tempfile | 32 import tempfile |
34 import urlparse | 33 import urlparse |
35 import shortuuid | 34 import shortuuid |
51 OPT_UPLOAD_IGNORE_HOST = 'upload_ignore_host' | 50 OPT_UPLOAD_IGNORE_HOST = 'upload_ignore_host' |
52 OPT_IGNORE_TLS = 'ignore_tls_errors' | 51 OPT_IGNORE_TLS = 'ignore_tls_errors' |
53 URL_REDIRECT_PREFIX = 'url_redirect_' | 52 URL_REDIRECT_PREFIX = 'url_redirect_' |
54 | 53 |
55 | 54 |
56 BlogImporter = collections.namedtuple('BlogImporter', ('callback', 'short_desc', 'long_desc')) | |
57 | |
58 | |
59 class BlogImportPlugin(object): | 55 class BlogImportPlugin(object): |
60 BOOL_OPTIONS = (OPT_UPLOAD_IMAGES, OPT_IGNORE_TLS) | 56 BOOL_OPTIONS = (OPT_UPLOAD_IMAGES, OPT_IGNORE_TLS) |
61 OPT_DEFAULTS = {OPT_UPLOAD_IMAGES: True, | 57 OPT_DEFAULTS = {OPT_UPLOAD_IMAGES: True, |
62 OPT_IGNORE_TLS: False} | 58 OPT_IGNORE_TLS: False} |
63 | 59 |
68 self._p = host.plugins['XEP-0060'] | 64 self._p = host.plugins['XEP-0060'] |
69 self._m = host.plugins['XEP-0277'] | 65 self._m = host.plugins['XEP-0277'] |
70 self._s = self.host.plugins['TEXT-SYNTAXES'] | 66 self._s = self.host.plugins['TEXT-SYNTAXES'] |
71 host.plugins['IMPORT'].initialize(self, u'blog') | 67 host.plugins['IMPORT'].initialize(self, u'blog') |
72 | 68 |
73 def importItem(self, client, item_import_data, options, return_data, service, node): | 69 def importItem(self, client, item_import_data, session, options, return_data, service, node): |
74 """importItem specialized for blog import | 70 """importItem specialized for blog import |
75 | 71 |
76 @param items_import_data(iterable[dict]): | 72 @param item_import_data(dict): |
77 * mandatory keys: | 73 * mandatory keys: |
78 'blog' (dict): microblog data of the blog post (cf. http://wiki.goffi.org/wiki/Bridge_API_-_Microblogging/en) | 74 'blog' (dict): microblog data of the blog post (cf. http://wiki.goffi.org/wiki/Bridge_API_-_Microblogging/en) |
79 the importer MUST NOT create node or call XEP-0277 plugin itself | 75 the importer MUST NOT create node or call XEP-0277 plugin itself |
80 'comments*' key MUST NOT be used in this microblog_data, see bellow for comments | 76 'comments*' key MUST NOT be used in this microblog_data, see bellow for comments |
81 It is recommanded to use a unique id in the "id" key which is constant per blog item, | 77 It is recommanded to use a unique id in the "id" key which is constant per blog item, |
122 log.info(u"url link from {old} to {new}".format( | 118 log.info(u"url link from {old} to {new}".format( |
123 old=old_uri, new=new_uri)) | 119 old=old_uri, new=new_uri)) |
124 | 120 |
125 return mb_data | 121 return mb_data |
126 | 122 |
127 def importSubItems(self, client, item_import_data, mb_data, options): | 123 def importSubItems(self, client, item_import_data, mb_data, session, options): |
128 # comments data | 124 # comments data |
129 if len(item_import_data['comments']) != 1: | 125 if len(item_import_data['comments']) != 1: |
130 raise NotImplementedError(u"can't manage multiple comment links") | 126 raise NotImplementedError(u"can't manage multiple comment links") |
131 allow_comments = C.bool(mb_data.get('allow_comments', C.BOOL_FALSE)) | 127 allow_comments = C.bool(mb_data.get('allow_comments', C.BOOL_FALSE)) |
132 if allow_comments: | 128 if allow_comments: |
141 else: | 137 else: |
142 if item_import_data['comments'][0]: | 138 if item_import_data['comments'][0]: |
143 raise exceptions.DataError(u"allow_comments set to False, but comments are there") | 139 raise exceptions.DataError(u"allow_comments set to False, but comments are there") |
144 return None | 140 return None |
145 | 141 |
146 def publishItem(self, client, mb_data, service, node): | 142 def publishItem(self, client, mb_data, service, node, session): |
147 log.debug(u"uploading item [{id}]: {title}".format(id=mb_data['id'], title=mb_data.get('title',''))) | 143 log.debug(u"uploading item [{id}]: {title}".format(id=mb_data['id'], title=mb_data.get('title',''))) |
148 return self._m.send(client, mb_data, service, node) | 144 return self._m.send(client, mb_data, service, node) |
149 | 145 |
150 @defer.inlineCallbacks | 146 @defer.inlineCallbacks |
151 def itemFilters(self, client, mb_data, options): | 147 def itemFilters(self, client, mb_data, session, options): |
152 """Apply filters according to options | 148 """Apply filters according to options |
153 | 149 |
154 modify mb_data in place | 150 modify mb_data in place |
155 @param posts_data(list[dict]): data as returned by importer callback | 151 @param posts_data(list[dict]): data as returned by importer callback |
156 @param options(dict): dict as given in [blogImport] | 152 @param options(dict): dict as given in [blogImport] |