public void insertRecentShow(ArchiveShowObj show) {
   insertShow(show);
   db.execSQL(
       "INSERT INTO recentShowsTbl(show_id) "
           + "SELECT show._id "
           + "FROM showTbl show "
           + "WHERE show.showIdent = '"
           + show.getIdentifier()
           + "' "
           + "AND NOT EXISTS (SELECT 1 FROM recentShowsVw recent WHERE recent.showIdent = '"
           + show.getIdentifier()
           + "')");
 }
 public void insertFavoriteShow(ArchiveShowObj show) {
   insertShow(show);
   db.execSQL(
       "INSERT INTO favoriteShowsTbl(show_id) "
           + "SELECT show._id "
           + "FROM showTbl show "
           + "WHERE show.showIdent = '"
           + show.getIdentifier()
           + "' "
           + "AND NOT EXISTS (SELECT 1 FROM favoriteShowsVw fav WHERE fav.showIdent = '"
           + show.getIdentifier()
           + "')");
 }
  public static void getSongs(
      ArchiveShowObj show,
      ArrayList<ArchiveSongObj> songs,
      StaticDataStore db,
      boolean processSongs) {

    HtmlCleaner pageParser = new HtmlCleaner();
    CleanerProperties props = pageParser.getProperties();
    props.setAllowHtmlInsideAttributes(true);
    props.setAllowMultiWordAttributes(true);
    props.setRecognizeUnicodeChars(true);
    props.setOmitComments(true);

    ArrayList<String> songLinks = new ArrayList<String>();
    ArrayList<String> songTitles = new ArrayList<String>();
    String showTitle = show.getArtistAndTitle();
    String showIdent = show.getIdentifier();

    // XPATH says "Select out of all 'table' elements with attribute 'class'
    // equal to 'fileFormats' which contain element 'tr'..."
    // String songXPath = "//table[@class='fileFormats']//tr";

    // XPATH says "Select out of all 'script' elements with attribute 'type'
    // equal to 'text/javascript'..."
    String m3uXPath = "//script";
    String titlePath = "//head//title";

    if (db.getShowExists(show) && processSongs) {

      songs.addAll(db.getSongsFromShow(show.getIdentifier()));
      show.setFullTitle(db.getShow(show.getIdentifier()).getArtistAndTitle());
      return;
    }

    try {
      HttpParams params = new BasicHttpParams();
      int timeout = (int) (15 * DateUtils.SECOND_IN_MILLIS);
      HttpConnectionParams.setConnectionTimeout(params, timeout);
      HttpConnectionParams.setSoTimeout(params, timeout);
      HttpClient client = new DefaultHttpClient(params);

      HttpGet page = new HttpGet(show.getShowURL().toString());
      HttpResponse pageResponse = client.execute(page);
      StatusLine pageStatus = pageResponse.getStatusLine();
      if (pageStatus.getStatusCode() == HttpStatus.SC_OK) {
        ResponseHandler<String> pageResponseHandler = new BasicResponseHandler();
        TagNode node = pageParser.clean(pageResponseHandler.handleResponse(pageResponse));

        String queryString = show.getLinkPrefix();

        if (db.getPref("downloadFormat").equalsIgnoreCase("LBR")) {
          if (show.hasLBR()) {
            queryString += "_64kb.m3u";
          } else if (show.hasVBR()) {
            queryString += "_vbr.m3u";
          }
        } else {
          if (show.hasVBR()) {
            queryString += "_vbr.m3u";
          } else if (show.hasLBR()) {
            queryString += "_64kb.m3u";
          }
        }

        HttpGet M3Urequest = new HttpGet(queryString);

        HttpResponse M3Uresponse = client.execute(M3Urequest);
        StatusLine M3Ustatus = M3Uresponse.getStatusLine();
        if (M3Ustatus.getStatusCode() == HttpStatus.SC_OK) {
          ResponseHandler<String> M3UresponseHandler = new BasicResponseHandler();
          String m3uString = M3UresponseHandler.handleResponse(M3Uresponse);

          client.getConnectionManager().shutdown();

          // Now split the .M3U file based on newlines. This will give
          // us the download links, which we store..

          String m3uLinks[] = m3uString.split("\n");
          for (String link : m3uLinks) {
            songLinks.add(link);
          }

          // Now use an XPATH evaluation to find all of the javascript scripts on the page.
          // If one of them can be split by "IAD.mrss = ", it should have the track names
          // in it. The second half of the split is valid javascript and can be interpreted,
          // therefore, as JSON. Pull the song titles out of that, and together with the
          // download links make ArchiveSongObjs and add them to the list of songs.
          Object[] titleNodes = node.evaluateXPath(m3uXPath);
          for (Object titleNode : titleNodes) {
            //
            List x = ((TagNode) titleNode).getChildren();
            String songTitle = "";
            for (Object y : x) {
              if (y instanceof ContentNode) {
                songTitle = ((ContentNode) y).toString();
                songTitle = songTitle.trim();
                if (songTitle.startsWith("Play(")) {
                  String[] titles = songTitle.split("\\{\"title\"");
                  for (int i = 1; i < titles.length; i++) {
                    try {
                      String title =
                          titles[i].substring(
                              nthIndexOf(titles[i], '"', 1), nthIndexOf(titles[i], '"', 2));
                      songTitles.add(title.substring(title.indexOf('.') + 2));
                    } catch (StringIndexOutOfBoundsException e) {
                    }
                  }
                }
              }
            }
          }
          if (show.getShowTitle().length() < 2) {

            String s =
                ((TagNode) node.evaluateXPath(titlePath)[0])
                    .getChildren()
                    .toString()
                    .replaceFirst(Pattern.quote("["), "");
            show.setFullTitle(s.substring(0, s.lastIndexOf(": Free") - 1));
            showTitle = show.getArtistAndTitle();
            db.updateShow(show);
          }

          if (processSongs) {
            if (songLinks.size() == 0) {

            } else {
              // Do things for successful show parse
              db.insertShow(show);
            }
            // If we have the same amount of song titles as song links,
            // we should be all set.
            if (songTitles.size() == songLinks.size()) {
              for (int i = 0; i < songTitles.size(); i++) {
                String songLink = songLinks.get(i);
                String songTitle = songTitles.get(i);
                // If the show has a "selectedSong"
                // meaning that it was opened by
                // the user clicking on a song link, do
                // a comparison to see
                // if the song being added is the
                // selected song. If it is, set
                // selectedPos to the right index so
                // that the song can be played
                // once the ListView is filled.  This is
                // inefficient, though it probably doesn't make a difference,
                // but we might consider making this a bit more efficient/elegant in the future.
                // FIXME.
                //						if (show.hasSelectedSong()) {
                //							if (songLink.equals(show.getSelectedSong())) {
                //								selectedPos = i;
                //							}
                //						} else {
                //							selectedPos = -1;
                //						}
                ArchiveSongObj song = new ArchiveSongObj(songTitle, songLink, showTitle, showIdent);
                song.setID(db.insertSong(song));
                songs.add(song);
              }
              db.setShowExists(show);
              db.insertRecentShow(show);
            } else {

            }
          }

        } else {
          client.getConnectionManager().shutdown();
        }
      } else {
        client.getConnectionManager().shutdown();
      }

    } catch (XPatherException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }

    // TODO Auto-generated method stub

  }