@Override public List<Video> fetchSource(String embedPageUrl) throws OnErrorThrowable { String body = GeneralUtils.getWebPage(embedPageUrl); String elementHtml = Jsoup.parse(body).select("div.progress").first().nextElementSibling().html(); String onlyJson = elementHtml.substring( elementHtml.indexOf("var vars = ") + 11, elementHtml.indexOf("var fixed_player_size")); List<Video> videos = new ArrayList<>(1); try { String regex = "url\\d+"; Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE); JsonParser jsonParser = new JsonFactory().createParser(onlyJson); while (!jsonParser.isClosed()) { JsonToken jsonToken = jsonParser.nextToken(); String currentName = jsonParser.getCurrentName(); if (jsonToken != null && currentName != null) { Matcher matcher = pattern.matcher(currentName); if (matcher.find()) { String nextValue = jsonParser.nextTextValue(); if (nextValue != null) { videos.add(new Video(currentName.substring(3) + "p", nextValue)); } } } } } catch (IOException io) { throw OnErrorThrowable.from(new Throwable("Vk video retrieval failed.", io)); } return videos; }
public void jsonParser() { int iterator = 0; Manager manager = Manager.getInstance(); // Create the object of singleton Element jacksonElement = new Element(); JsonFactory jsonFactory = new JsonFactory(); try { File f = new File(FileDownload.getFileName()); JsonParser parser = jsonFactory.createParser(f); // Parse the file by Jackson parser while (!parser.isClosed()) { JsonToken jsonToken = parser.nextToken(); if (JsonToken.FIELD_NAME.equals(jsonToken)) { String fieldName = parser.getCurrentName(); jsonToken = parser.nextToken(); // The JSON file has the next fields: // location,date,name and object stock (this object // includes:name,bid,id,visible,minPrice,maxPrice) if ("location".equals(fieldName)) { manager.setLocation(parser.getValueAsString()); } if ("date".equals(fieldName)) { manager.setDate(parser.getValueAsString()); } if ("name".equals(fieldName) && (parser.getValueAsString().equals("Moscow Stock"))) manager.setName(parser.getValueAsString()); if ("bid".equals(fieldName)) { jacksonElement.setBid(parser.getValueAsDouble()); iterator++; } else if ("id".equals(fieldName)) { jacksonElement.setId(parser.getValueAsInt()); iterator++; } else if ("name".equals(fieldName) && (!parser.getValueAsString().equals("Moscow Stock"))) { jacksonElement.setName( parser.getValueAsString().substring(0, parser.getValueAsString().length() - 4)); iterator++; } else if ("minPrice".equals(fieldName)) { jacksonElement.setMinPrice(parser.getValueAsDouble()); iterator++; } else if ("maxPrice".equals(fieldName)) { jacksonElement.setMaxPrice(parser.getValueAsDouble()); iterator++; } else if ("visible".equals(fieldName)) { jacksonElement.setVisible(parser.getValueAsBoolean()); iterator++; } if (iterator == 6) { manager.setJsonList(jacksonElement); // add the data to the list jacksonElement = new Element(); iterator = 0; } } } } catch (JsonParseException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }