adding Intervals for Fetching RSS Data

edited December 2013 in Questions about Code

Hi , I've made a sketch to Fetch data from RSS Feed and save them in PNG format , Now I want to add interval to be able to fetch every 5 minutes , can any one help me with the code :

PGraphics pg;
String[] titles;
PFont font;
int rows = 7 ;

void setup() {
  pg = createGraphics(1280 , 720 );
  size(1280, 720);
  pg.smooth();


}

void draw() {

  xmlparse();

  pg.beginDraw();

  PFont f = createFont("Nazanin", 48);
  pg.textFont(f, 48);

  // Draw all titles with bars and colors depending on its length
  for (int i = 0; i < rows ; i++) {
    float y = (i+1) * 65;


     pg.fill(0);

    pg.text(titles[i],  width - 5, y);
    pg.textAlign(RIGHT , TOP);

  }  

  pg.endDraw();

  image(pg, 0, 0, width, height);

   pg.save("news.png");
}


void xmlparse(){



  // Load RSS feed
  String url = "http://www.hamshahrionline.ir/rss";
  XML rss = loadXML(url);
  // Get title of each element
  XML[] titleXMLElements = rss.getChildren("channel/item/title");
  titles = new String[titleXMLElements.length];

  for (int i = 0; i < rows ; i++) {
    String title = titleXMLElements[i].getContent();
    // Store title in array for later use
    titles[i] = title;
  }


}

Answers

  • edited December 2013 Answer ✓

    What about setting a separate Thread w/ the task to retrieve RSS data within a delay(INTERVAL)? *-:)
    Introducing Processing's hidden & unofficial function -> thread("")! =D>
    With that, we can call a defined function within our sketch as an independent running Thread!
    I've also made lotsa tweaks to your code. Check it out: ;;)

    /** 
     * Timed XML Fetcher (v2.11)
     * by  alless0 (2013/Dec)
     * mod GoToLoop
     * 
     * forum.processing.org/two/discussion/1891/
     * adding-intervals-for-fetching-rss-data
     */
    
    static final int MAX_ROWS = 9, INTERVAL = 5*60*1000; // 5 minutes
    static final int DIM = 48, GAP = 65;
    static final color BG = 0300, FG = 0;
    
    static final String URL  = "http://www.hamshahrionline.ir/rss";
    static final String DIR  = "channel/item/title";
    static final String NAME = "news-", EXT = ".png";
    
    static PGraphics pg;
    static boolean isActive = true;
    
    void setup() {
      size(1280, 720);
      frameRate(60);
      noLoop();
    
      pg = createGraphics(width, height);
    
      pg.beginDraw();
      pg.smooth(4);
      pg.fill(FG);
      pg.textFont(createFont("Nazanin", DIM));
      pg.textAlign(RIGHT, TOP);
      pg.endDraw();
    
      thread("xmlFetchTimer");
    }
    
    void draw() {
      background(pg);
      //pg.save(dataPath(NAME + nf(frameCount, 3) + EXT));
      saveFrame(dataPath(NAME + "###" + EXT));
    }
    
    void xmlGetAndRender() {
      final XML[] elems = loadXML(URL).getChildren(DIR);
      final int len = min(elems.length, MAX_ROWS);
      final int w = width - 5;
    
      pg.background(BG);
      for (int i = 0; i != len; pg.text(elems[i++].getContent(), w, GAP*i));
      pg.endDraw();
    }
    
    void xmlFetchTimer() {
      delay(1000);
      frameCount = 1;
    
      while (isActive) {
        xmlGetAndRender();
        redraw();
        delay(INTERVAL);
      }
    }
    
  • Wow ! Thanx Man Your a Life saver :)

Sign In or Register to comment.