def get_sanitize_content(self, content): """ tidy and sanitize content """ enml = sanitize(content) # python 2 if sys.version_info.major == 2: return enml.encode('ascii', 'xmlcharrefreplace') else: return str(enml)
def _content(note, content): """ content of the note :param note: note object :param content: content string to make the main body of the note :return: """ note.content = EvernoteMgr.set_header() note.content += sanitize(content) return note
def test_sanitize(self): html = "<html><body>" \ "<p>coucou</p>" \ "<dir>foobar</dir>" \ "<div data-foobar='nothing'>foobar2</div>" \ "<a href='ftp://localhost'>dropped</a>" \ "<a href='http://localhost'>kept</a>" \ "</body></html>" html = sanitize(html) self.assertTrue("dir" not in html) self.assertTrue("ftp" not in html) self.assertTrue("data-foobar" not in html) self.assertTrue("http" in html)
def save_data(self, token, trigger_id, **data): """ let's save the data dont want to handle empty title nor content otherwise this will produce an Exception by the Evernote's API """ content = '' if 'content' in data: if type(data['content']) is list: if 'value' in data['content'][0]: content = data['content'][0].value else: if 'value' in data['content']: content = data['content']['value'] else: content = data['content'] elif 'summary_detail' in data: if type(data['summary_detail']) is list: if 'value' in data['summary_detail'][0]: content = data['summary_detail'][0].value else: if 'value' in data['summary_detail']: content = data['summary_detail']['value'] else: content = data['summary_detail'] elif 'description' in data: content = data['description'] # if no title provided, fallback to the URL which should be provided # by any exiting service title = (data['title'] if 'title' in data else data['link']) if token and len(title): # get the evernote data of this trigger trigger = Evernote.objects.get(trigger_id=trigger_id) client = EvernoteClient( token=token, sandbox=settings.TH_EVERNOTE['sandbox']) # user_store = client.get_user_store() note_store = client.get_note_store() # note object note = Types.Note() if trigger.notebook: notebooks = note_store.listNotebooks() listtags = note_store.listTags() notebookGuid = 0 tagGuid = [] # get the notebookGUID ... for notebook in notebooks: if notebook.name.lower() == trigger.notebook.lower(): notebookGuid = notebook.guid break #... and get the tagGUID if a tag has been provided if trigger.tag is not '': # cut the string by piece of tag with comma if ',' in trigger.tag: for my_tag in trigger.tag.split(','): for tag in listtags: # remove space before and after # thus we keep "foo bar" # but not " foo bar" nor "foo bar " if tag.name.lower() == my_tag.lower().lstrip().rstrip(): tagGuid.append(tag.guid) break else: for tag in listtags: if tag.name.lower() == my_tag.lower(): tagGuid.append(tag.guid) break # notebookGUID does not exist: # create it if notebookGuid == 0: new_notebook = Types.Notebook() new_notebook.name = trigger.notebook new_notebook.defaultNotebook = False note.notebookGuid = note_store.createNotebook( new_notebook).guid else: note.notebookGuid = notebookGuid # tagGUID does not exist: # create it if a tag has been provided if tagGuid == 0 and trigger.tag is not '': new_tag = Types.Tag() new_tag.name = trigger.tag tagGuid = note_store.createTag(new_tag).guid if trigger.tag is not '': # set the tag to the note if a tag has been provided note.tagGuids = tagGuid logger.debug("notebook that will be used %s", trigger.notebook) if 'link' in data: # add the link of the 'source' in the note # get a NoteAttributes object na = Types.NoteAttributes() # add the url na.sourceURL = data['link'] # add the object to the note note.attributes = na # will add this kind of info in the footer of the note : # "provided by FoxMaSk's News from http://domain.com" # domain.com will be the link and the text of the link provided_by = _('Provided by') provided_from = _('from') footer_from = "<br/><br/>{} <em>{}</em> {} <a href='{}'>{}</a>" footer = footer_from.format( provided_by, trigger.trigger.description, provided_from, data['link'], data['link']) content += footer # start to build the "note" # the title note.title = title # the body prolog = '<?xml version="1.0" encoding="UTF-8"?>' prolog += '<!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd">' note.content = prolog # tidy and sanitize content enml = sanitize(content) note.content += str(enml) # create the note ! try: created_note = note_store.createNote(note) sentance = str('note %s created') % created_note.guid logger.debug(sentance) except Exception as e: logger.critical(e) else: sentence = "no token provided for trigger ID {} and title {}" logger.critical(sentence.format(trigger_id, title))