The guardian Api

edited April 2015 in How To...

Hi, I would like to use the guardian API I have a key but I don't know how to start in processing. All the examples I can find are outdated using libraries that can't be use with Processing 0.3. Really appreciate any help.

Tagged:

Answers

  • Why can't they be used with Processing 3?

  • they use processing.xml

  • https://www.processing.org/reference/loadXML_.html

    https://www.processing.org/reference/XML.html

    can you post a xml code you got from guardian, then we can take a look?

    ;-)

  • Thanks for your reply .This is what I have been looking at http://www.theguardian.com/open-platform/blog/the-truth-is-in-there

  • edited April 2015

    as said, since I don't have the api key, please post a result of a query

  • I see, you don't get a result

    sorry

  • edited April 2015
    /*
    
     Getting Started with the Gaurdian Content API
     Sample file for this post: 
    
     April, 2009
     blprnt@blprnt.com
    
     http: // forum.processing.org/two/discussion/10422/the-guardian-api
    
     http: // www.theguardian.com/open-platform/blog/the-truth-is-in-there
    
     ***** Make sure you enter your API Key on the GuardianArticleSearch API
    
     */
    
    //This is the setup wrapper. Any instructions we put in here will run once, when the program is started.
    void setup() {
    
      GuardianArticleSearch mySearch = new GuardianArticleSearch(); 
    
      // mySearch.addQueries("FBI");
      mySearch.addQueries("NASA");
    
      GuardianArticleSearchResult myResult = mySearch.doSearch(); 
    
      println("count " + myResult.count);
    }
    
    
    //This is the draw wrapper. Any instructions we put in here will over and over again, as long as the program is running.
    void draw() {
    
      // empty
    }
    //
    
    
    // -------------------------------------
    
    
    /*
    
     TimesArticleSearch Class
    
     February, 2009
     blprnt@blprnt.com
    
     */
    
    public class GuardianArticleSearch {
    
      ArrayList queries = new ArrayList();
      ArrayList filters = new ArrayList();
    
      String after;
      String before;
    
      int count = 10;
    
      String baseURL = "http"
            +"://"
            +content.guardianapis.com/search";
      String apiKey = "xxxxxxxxxxxxxxxxxxxxxxxxxx";   
    
      import java.net.URLEncoder;
    
    
      /*
      *
       *  Constructor
       *
       */
      //  GuardianArticleSearch() {
      //  };
    
      /*
      *
       * Search functions - return TimesArticleSearchResults
       *
       */
      GuardianArticleSearchResult doSearch() {
    
        GuardianArticleSearchResult dc = new GuardianArticleSearchResult();
        String url = constructURL();
    
        println(url);
        println("***********************");
    
        XML xml;
    
        boolean done = false;
        int c = 0;
    
        while (!done && c < 10) {
          c++;
          try {
    
            // URL myUrl = new URL(url);
    
            //        HttpURLConnection myConn = (HttpURLConnection) myUrl.openConnection();
            //        myConn.setFollowRedirects(true);
            //        myConn.setRequestProperty("User-Agent", "Mozilla/4.76");
            //        BufferedReader br = new BufferedReader(new InputStreamReader(myConn.getInputStream()));
            //        xml = new XMLElement(br);
            //        dc.processResults(xml);
    
            xml=loadXML(url);
            done = true;
            println("step 1 ***********************");
            println(xml);
            println("***********************");
    
            dc.processResults(xml); // the core I guess
          }
          catch (Exception e) {
            println("EXCEPTION " + e);
          };
        };
    
        return(dc);
      };
    
      /*
      *
       * Search modifying functions
       *
       */
      void addQueries(String q) {
        String[] qa = split(q, ",");
        for (int i = 0; i < qa.length; i++) {
          queries.add(queries.size(), URLEncoder.encode(qa[i]));
        };
      };
    
      void clearQueries() {
        queries = new ArrayList();
      };
    
      void addFilters(String q) {
        String[] qa = split(q, ",");
        for (int i = 0; i < qa.length; i++) {
          filters.add(filters.size(), qa[i]);
        };
      };
    
      void clearFilters() {
        filters = new ArrayList();
      };
    
    
    
      /*
      *
       * URL construction
       *
       */
      String constructURL() {
        String url;
    
        if (after != null) {
          // mostly not in use???
          url = baseURL + "?q=" + getQueries() +  getFilters()  + "&after=" + after + "&before=" + before + "&format=xml&count=" + count + "&api-key=" + apiKey;
        } else {
          // the real line 
          url = baseURL + "?q=" + getQueries() +  getFilters()  + "&format=xml&count=" + count + "&api-key=" + apiKey;
          // test line
          // url = baseURL + "?" + "format=xml&api-key=" + apiKey;
        };
    
        return(url);
      };
    
      String getQueries() {
        String q = "";
        for (int i = 0; i < queries.size (); i++) {
          q += (String) queries.get(i);
          if (i < queries.size() - 1) q += ",";
        };
        return(q);
      };
    
      String getFilters() {
        String q = "";
        for (int i = 0; i < filters.size (); i++) {
          if (i == 0) q += "&";
          q += "filter=" + (String) filters.get(i);
          if (i < queries.size() - 1) q += "&";
        };
    
        return(q);
      };
    };
    
    
    //----------------------------------
    
    /*
    
     TimesArticleSearchResult Class
    
     February, 2009
     blprnt@blprnt.com
    
     <search count="4895" start-index="0">
     <results>
     <content>
     </results>
     </search>
    
     */
    
    
    public class GuardianArticleSearchResult {
    
      GuardianContentObject[] content = {
      };
      int count = 0;
    
      GuardianTagObject[] tags = {
      };
    
    
      //  GuardianArticleSearchResult() {
      //  };
    
      void processResults(XML xml) {
    
        // ????????????????
        // count = xml.getInt("count");
        count = xml.getInt("total");
    
        XML results = xml.getChild(0);
        int rc = results.getChildCount();
        content = new GuardianContentObject[rc];
        for (int i = 0; i < rc; i++) {
          GuardianContentObject gc = new GuardianContentObject();
          gc.processXML( results.getChild(i));
          content[i] = gc;
        };
    
        XML filters = xml.getChild(1);
        int fc = filters.getChildCount();
        tags = new GuardianTagObject[fc];
        for (int j = 0; j < fc; j++) {
          GuardianTagObject to = new GuardianTagObject();
          to.processXML( filters.getChild(j));
          tags[j] = to;
        };
      };
    };
    
    
    // -----------------------------
    
    class GuardianContentObject {
    
      String id;
      String gtype;
      String web_url;
      String api_url;
    
      //  GuardianContentObject() {
      //
      // };
    
      GuardianTagObject[] tags = {
      };
    
      void processXML(XML xml) {
    
        id = xml.getString("id");
        gtype = xml.getString("type");
        web_url = xml.getString("web-url");
        api_url = xml.getString("api-url");
    
        XML taggedWith = xml.getChild(8);
        int tc = taggedWith.getChildCount();
    
        tags = new GuardianTagObject[tc];
    
        for (int i = 0; i < tc; i ++) {
          GuardianTagObject t = new GuardianTagObject();
          t.processXML(taggedWith.getChild(i));
          tags[i] = t;
        };
      };
    };
    
    
    //--------------
    
    class GuardianTagObject {
    
      String name;
      String type;
      String gfilter;
      String api_url;
      String web_url;
    
      //  GuardianTagObject() {
      //  };
    
      void processXML(XML xml) {
    
        name = xml.getString("name");
        type = xml.getString("type");
        gfilter = xml.getString("filter");
        api_url = xml.getString("api-url");
        web_url = xml.getString("web-ut");
      }
    }
    
  • up and running...

    the xml handling is completely new (and more easy) in processing now

    also guardian changed the url

    and changed count to total

    on the page you posted a lot of the code is missing

    you could write them or search the forum there

    ;-)

  • Hi,thanks. what you copy gave me the error "unexpected token" in here:

    String baseURL = "http://content.guardianapis.com/search";

    could you give me an example of missing code?

  • the forum is destroying links like this

    insert the api key

    it worked for me

  •  String baseURL = "http"
            +"://"
            +content.guardianapis.com/search";
    
  • (forum also destroys multi-line comments unless you start them with /** (two stars))

  • edited April 2015

  • yes, as said replace the yellow line with

    String baseURL = "http"
           +"://"
           +content.guardianapis.com/search";
    
  • Answer ✓

    you could PM me your e-mail-adress and I can send you my zip-file tonight

    I had to put all tabs into one since the forum doesn't allow uploads of zips

    just read the code carefully; the yellow line just doesn't make sense ...

    ;-)

  • nmnnmn
    edited August 2015

    Shouldn't it be

    String baseURL = "http"
    +"://"
    +"content.guardianapis.com/search";

    ? That's the only way I can get it to work...

  • yes I mentioned that twice above

Sign In or Register to comment.