Search in sources :

Example 1 with StringRankFilter

use of com.github.mob41.osums.search.StringRankFilter in project osumer by mob41.

the class OsumsServer method downloadBasicSongResults.

public void downloadBasicSongResults() throws Exception {
    logger.info("[Stage 1/2] Downloading all results from all rank status...");
    String[] ranks = { OsumsNewParser.RANK_LOVED, OsumsNewParser.RANK_PENDING, OsumsNewParser.RANK_QUALIFIED, OsumsNewParser.RANK_RANKED };
    SearchResult result;
    SearchFilter[] filters;
    String rank;
    List<SongResult> list = new ArrayList<SongResult>();
    for (int i = 0; i < ranks.length; i++) {
        rank = ranks[i];
        logger.info("[Stage 1/2] Requesting results from \"" + rank + "...");
        filters = new SearchFilter[] { new StringRankFilter(rank) };
        int page = 1;
        do {
            logger.info("[Stage 1/2] [" + rank + "] Requesting results from page " + page + "...");
            result = osums.searchOnlineMaps(null, filters, page);
            if (result == null) {
                logger.error("[Stage 1/2] [" + rank + "] No results returned at " + page + "!");
                continue;
            }
            list.addAll(Arrays.asList(result.getResult()));
            logger.info("[Stage 1/2] [" + rank + "] Completed fetching results from page " + page + ". (" + page + "/" + result.getTotalPages() + ") (" + (int) (Math.floor((page / (float) result.getTotalPages() + i) / (float) ranks.length * 100.0)) + "%)");
            if (result.getCurrentPage() < result.getTotalPages()) {
                page++;
            }
        } while (result != null && result.getCurrentPage() < result.getTotalPages());
    }
    logger.info("[Stage 2/2] Importing all results to database...");
    int error = 0;
    PreparedStatement pst = conn.prepareStatement("INSERT INTO songs (id, rank, artist, title, creator, tags, favourites, plays) VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
    SongResult r;
    for (int i = 0; i < list.size(); i++) {
        r = list.get(i);
        logger.info("[Stage 2/2] Importing " + r.getId() + " (" + (i + 1) + "/" + list.size() + ") (" + (int) (Math.floor(i / (float) list.size() * 100)) + "%)");
        pst.setInt(1, r.getId());
        pst.setString(2, r.getRank());
        pst.setString(3, r.getArtist());
        pst.setString(4, r.getTitle());
        pst.setString(5, r.getCreator());
        pst.setString(6, join(r.getTags(), ","));
        pst.setInt(7, r.getFavourites());
        pst.setInt(8, r.getPlays());
        try {
            pst.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
            error++;
        }
    }
    pst.close();
    logger.info("SQL Statements ended with " + error + " errors.");
    logger.info("All basic song results have been downloaded and imported.");
    prop.put(KEY_DOWNLOAD_BASIC_RESULTS, "false");
    save();
}
Also used : SongResult(com.github.mob41.osums.search.SongResult) ArrayList(java.util.ArrayList) SearchResult(com.github.mob41.osums.search.SearchResult) SearchFilter(com.github.mob41.osums.search.SearchFilter) StringRankFilter(com.github.mob41.osums.search.StringRankFilter) PreparedStatement(java.sql.PreparedStatement) SQLException(java.sql.SQLException) IOException(java.io.IOException)

Aggregations

SearchFilter (com.github.mob41.osums.search.SearchFilter)1 SearchResult (com.github.mob41.osums.search.SearchResult)1 SongResult (com.github.mob41.osums.search.SongResult)1 StringRankFilter (com.github.mob41.osums.search.StringRankFilter)1 IOException (java.io.IOException)1 PreparedStatement (java.sql.PreparedStatement)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1