Read twitter user status

edited May 2017 in Share Your Work
String url = "https://" + "twitter.com/*********";//USER URL
String[] lines = loadStrings(url);
String[] status;
String actual_stat;
// Get rid of the array in order to search the whole page

for (int i = 0 ; i < lines.length - 1 ; i++) {
  if (lines[i].indexOf("data-permalink-path=") == 0) {
    //println(lines[i]);
    println("************************************************");
    actual_stat=lines[i].substring(lines[i].indexOf("/"), lines[i].length() - 1);
    //status = loadStrings("https://" + "twitter.com" + lines[i]);

    //println("https://twitter.com" + actual_stat);
    status = loadStrings("https://" + "twitter.com" + actual_stat);
    //println(status);
    for (int j = 0 ; j < status.length ; j++) {
      if (status[j].indexOf("Twitter: &quot") > 0) {        
        if (status[j].indexOf("<title>") > 0) {
          //println(status[j].substring(status[j].indexOf("&quot") + 6, status[j].indexOf("</title>") - 6));  
          println(status[j].substring(status[j].indexOf("<title>") + 7, status[j].indexOf("</title>") - 6)); 
        }
      }
      //println(status[j].indexOf("<a href=" + '"' + actual_stat));
      if (status[j].indexOf("tweet-timestamp js-permalink js-nav js-tooltip") > 0 && status[j].indexOf("<a href=" + '"'  + actual_stat) > -1) {
          println(status[j].substring(status[j].indexOf("title=") + 7, status[j].indexOf("data-conversation") - 3));    
       }
    }
    println("************************************************");
  }
}

Comments

  • edited May 2017

    @FertheBana -- thank you for sharing this!

    To prevent this forum formatting from breaking your code, please edit your post above and change any line containing a URL to split the string, like this:

    String url = "https://" + "twitter.com/*********"
    
    status = loadStrings("https://" + "twitter.com" + actual_stat);
    
  • (done)

    needs more comments imo

  • @jeremydouglass thanks, i'll edit it

  • edited May 2017

    A new version ready for make some kind of twitter alarm

    String url = "https://" + "twitter.com/********";//USER URL
    String[] lines;
    String[] status;
    String actual_stat;
    // Get rid of the array in order to search the whole page
    String actualTime;
    
    ArrayList<twstatus>  AllStatus=new ArrayList<twstatus>();
    
    
    void setup(){
    
      size(640, 360);
      lines = loadStrings(url);
      getAllStatus();
      noLoop(); 
    }
    
    void draw(){
     background(0);
     textSize(14);
    
     actualTime=str(hour())+":"+str(minute()) + " - " +str(day())+ " " +Smonth()+" "+str(year()) ;
     text(AllStatus.get(0).getTwmsg(),10,180);  
     text(AllStatus.get(0).getTwdate(),10,200); 
     text(actualTime, 10,220); 
    }
    
    void getAllStatus(){
      for (int i = 0 ; i < lines.length - 1 ; i++) {
        if (lines[i].indexOf("data-permalink-path=") == 0) {
          //println(lines[i]);
          println("************************************************");
          actual_stat=lines[i].substring(lines[i].indexOf("/"), lines[i].length() - 1);
          //status = loadStrings("https://" + "twitter.com" + lines[i]);
    
          //println("<a href="https://twitter.com" target="_blank" rel="nofollow">https://twitter.com</a>;" + actual_stat);
          status = loadStrings("https://" + "twitter.com" + actual_stat);
          //println(status);
          AllStatus.add(new twstatus());      
          for (int j = 0 ; j < status.length ; j++) {
            if (status[j].indexOf("Twitter: &quot") > 0) {
    
    
              if (status[j].indexOf("<title>") > 0) {
                //println(status[j].substring(status[j].indexOf("&quot") + 6, status[j].indexOf("</title>") - 6));
                String msg=status[j].substring(status[j].indexOf("<title>") + 7, status[j].indexOf("</title>") - 6);
                println(msg); 
                AllStatus.get(AllStatus.size()-1).setTwmsg(msg);
              }
            }
            //println(status[j].indexOf("<a href=" + '"' + actual_stat));
            if (status[j].indexOf("tweet-timestamp js-permalink js-nav js-tooltip") > 0 && status[j].indexOf("<a href=" + '"'  + actual_stat) > -1) {
               String dte= status[j].substring(status[j].indexOf("title=") + 7, status[j].indexOf("data-conversation") - 3);
               println(dte);
               AllStatus.get(AllStatus.size()-1).setTwdate(dte);
             }
          }
          println("************************************************");
        }
      }
    }
    
    
    void mousePressed() {
    }
    
    String Smonth(){
    
      int mnth=month();
    
      String smnth="what!";
    
      switch (mnth){
       case 1: smnth="ene.";
               break;
       case 2: smnth="feb.";
               break;
       case 3: smnth="mar.";
               break;
       case 4: smnth="abr.";
               break;
       case 5: smnth="may.";
               break;
       case 6: smnth="jun.";
               break;
       case 7: smnth="jul.";
               break;
       case 8: smnth="ago.";
               break;
       case 9: smnth="sept.";
               break;
       case 10: smnth="oct.";
               break;
       case 11: smnth="nov.";
               break;
       case 12: smnth="dic.";
               break;
       default:
               break;
       }
    
       return smnth;
    
    }
    
    
    class twstatus{
      String twmsg;
      String twdate;
    
      twstatus(){
    
    
    
      }
    
      void setTwmsg(String inmsg){
        twmsg=inmsg; 
      }
      void setTwdate(String indate){
        twdate=indate;
      }  
    
      String getTwmsg(){
        return twmsg;  
      }
      String getTwdate(){
        return twdate;  
      }  
    
    }
    
Sign In or Register to comment.