我们可以在onenote API中搜索或过滤“data-tag ='待办事项'”吗?如果是,那么我们如何做到这一点?

时间:2016-09-19 07:57:39

标签: onenote onenote-api

我们如何在OneNote API中使用OneNote标记(如data-tag='to-do')与搜索或过滤器。我尝试使用提供运营商,但没有成功。

我试过这种方式 -

    $url = "https://www.onenote.com/api/v1.0/me/notes";
    //$url .= "/pages?search=hello";
    $url .= "/pages?filter=data-tag eq 'to-do'";

我想搜索数据标记,然后从包含data-tag ='待办事项'的OneNote页面中提取数据。

任何帮助都表示赞赏,并提前致谢。

2 个答案:

答案 0 :(得分:0)

您必须浏览所有网页。

对于每个页面,您可以通过GET调用https://www.onenote.com/api/v1.0/me/notes/pages/%s/content?includeIds=true

来检索其内容

从那里你得到一个你可以解析的字符串。

我建议您使用jsoup

使用jsoup,您可以编写(假设content包含您网页的内容):

Document doc = Jsoup.parse(content);
Elements todos=doc.select("[data-tag^=\"to-do\"]");

for(Element todo:todos) {
    System.out.println(todo.ownText());
    }

答案 1 :(得分:0)

可悲的是OneNote API还不支持它,所以我编写了自定义解析器,它从页面内容中提取带有数据标签的注释。这是:

public class OneNoteParser
    {
        static public List<Note> ExtractTaggedNotes(string pageContent, string tag = "*")
        {
            List<Note> allNotes = new List<Note>();

            string[] dataTagString = { "data-tag=\""};

            string[] dirtyNotes = pageContent.Split(dataTagString, StringSplitOptions.RemoveEmptyEntries);

            //First one in this array can be dropped as it doesn't contain todo
            for (int i = 1; i < dirtyNotes.Length; i  )
            {
                string curStr = dirtyNotes[i];
                Note curNote = new Note();

                // Firstly we need to extract all the tags from it (sample html: data-tag="to-do:completed,important" ....)
                string allTags = curStr.Substring(0,curStr.IndexOf("\""));

                curNote.Tags = new List<string>(allTags.Split(','));

                // Now we have to jump to the next ">" symbol and start finding the text after it
                curStr = curStr.Substring(curStr.IndexOf(">"));

                int depth = 1;
                bool addAllowed = false;

                for (int j = 0; j < curStr.Length - 1; j  )
                {
                    // Finding next tag opener "<" symbol
                    if (curStr[j] == '<')
                    {
                        addAllowed = false;

                        // Checking if it is not "</" closer
                        if (curStr[j   1] == '/')
                        {
                            // Means this is a tag closer. Decreasing depth
                            depth--;
                        }
                        else
                        {
                            // Means this is an tag opener. Increasing depth
                            depth  ;
                        }
                    }
                    else if (curStr[j] == '>')
                    {
                        addAllowed = true;

                        if (j > 0 && curStr[j - 1] == '/')
                        {
                            // Means this is a tag closer. Decreasing depth
                            depth--;
                        }
                    }
                    else
                    {
                        if (depth < 1)
                        {
                            // Found end of the tag. Saving index and exiting for loop
                            break;
                        }

                        if (addAllowed)
                            curNote.Text  = curStr[j]; // Appending letter to string
                    }
                }

                // Filtering by tag and adding to final list
                if (tag == "*" || curNote.Tags.Any(str => str.Contains(tag)))//curNote.Tags.Contains(tag, StringComparer.CurrentCultureIgnoreCase))
                        allNotes.Add(curNote);

            }
            return allNotes;
        }
    }

这是班级Note

   public class Note
    {
        public string Text;
        public List<string> Tags;
        public Note()
        {
            Tags = new List<string>();
        }
    }

要提取todo-s,只需调用此函数:

OneNoteParser.ExtractTaggedNotes(pageContent, "to-do");

您还可以提取其他标签:

OneNoteParser.ExtractTaggedNotes(pageContent, "important");
OneNoteParser.ExtractTaggedNotes(pageContent, "highlight");
//...