【问题标题】:Can we search or filter " data-tag='to-do' " in onenote API ? If yes then how we can do this?我们可以在 onenote API 中搜索或过滤“data-tag='to-do'”吗?如果是,那么我们怎么能做到这一点?
【发布时间】:2017-01-26 01:00:49
【问题描述】:

我们如何在 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,然后从包含data-tag='to-do'的OneNote页面中提取数据。

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

【问题讨论】:

    标签: onenote onenote-api


    【解决方案1】:

    您必须浏览所有页面。

    对于每个页面,您可以通过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());
        }
    

    【讨论】:

      【解决方案2】:

      遗憾的是 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>();
              }
          }
      

      要提取待办事项,只需调用此函数:

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

      你也可以像这样提取其他标签:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-06-07
        • 1970-01-01
        • 2011-12-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多