Search in sources :

Example 1 with Device

use of forpdateam.ru.forpda.api.devdb.models.Device in project ForPDA by RadiationX.

the class DevDb method getDevice.

public Device getDevice(String devId) throws Exception {
    Device data = new Device();
    NetworkResponse response = Api.getWebClient().get("https://4pda.ru/devdb/" + devId);
    Matcher matcher = Device.PATTERN_1.matcher(response.getBody());
    if (matcher.find()) {
        data.setTitle(matcher.group(1));
        Matcher matcher1 = Device.IMAGES_PATTERN.matcher(matcher.group(2));
        while (matcher1.find()) {
            data.addImage(matcher1.group(2), matcher1.group(1));
        }
        matcher1 = Device.SPECS_TITLED_PATTERN.matcher(matcher.group(3));
        while (matcher1.find()) {
            String title = ApiUtils.fromHtml(matcher1.group(1));
            Matcher specsMatcher = SPECS_PATTERN.matcher(matcher1.group(2));
            ArrayList<Pair<String, String>> specs = new ArrayList<>();
            while (specsMatcher.find()) {
                specs.add(new Pair<>(specsMatcher.group(1), specsMatcher.group(2)));
            }
            data.addSpecs(title, specs);
        }
    }
    matcher = MAIN_PATTERN.matcher(response.getBody());
    if (matcher.find()) {
        Matcher bcMatcher = BREADCRUMB_PATTERN.matcher(matcher.group(1));
        while (bcMatcher.find()) {
            if (bcMatcher.group(2) == null) {
                data.setCatId(bcMatcher.group(1));
                data.setCatTitle(bcMatcher.group(3));
            } else {
                data.setBrandId(bcMatcher.group(2));
                data.setBrandTitle(bcMatcher.group(3));
            }
        }
        if (matcher.group(2) != null) {
            data.setRating(Integer.parseInt(matcher.group(2)));
        }
        data.setTitle(matcher.group(4));
        data.setId(devId);
    }
    matcher = Device.COMMENTS_PATTERN.matcher(response.getBody());
    while (matcher.find()) {
        Device.Comment comment = new Device.Comment();
        comment.setId(Integer.parseInt(matcher.group(1)));
        comment.setRating(Integer.parseInt(matcher.group(3)));
        comment.setUserId(Integer.parseInt(matcher.group(4)));
        comment.setNick(ApiUtils.fromHtml(matcher.group(5)));
        comment.setDate(matcher.group(6));
        String text = matcher.group(9);
        if (text == null) {
            text = matcher.group(7);
        }
        comment.setText(text.trim());
        comment.setLikes(Integer.parseInt(matcher.group(10)));
        comment.setDislikes(Integer.parseInt(matcher.group(11)));
        data.addComment(comment);
    }
    matcher = Device.REVIEWS_PATTERN.matcher(response.getBody());
    while (matcher.find()) {
        Device.PostItem item = new Device.PostItem();
        item.setId(Integer.parseInt(matcher.group(1)));
        item.setImage(matcher.group(2));
        item.setTitle(ApiUtils.fromHtml(matcher.group(3)));
        item.setDate(matcher.group(4));
        if (matcher.group(5) != null) {
            item.setDesc(ApiUtils.fromHtml(matcher.group(5)));
        }
        data.addNews(item);
    }
    matcher = Device.DISCUSSIONS_PATTERN.matcher(response.getBody());
    if (matcher.find()) {
        Matcher matcher1 = Device.DISCUSS_AND_FIRM_PATTERN.matcher(matcher.group(1));
        while (matcher1.find()) {
            Device.PostItem item = new Device.PostItem();
            item.setId(Integer.parseInt(matcher1.group(1)));
            item.setTitle(ApiUtils.fromHtml(matcher1.group(2)));
            item.setDate(matcher1.group(3));
            if (matcher1.group(4) != null) {
                item.setDesc(ApiUtils.fromHtml(matcher1.group(4)));
            }
            data.addDiscussion(item);
        }
    }
    matcher = Device.FIRMwARES_PATTERN.matcher(response.getBody());
    if (matcher.find()) {
        Matcher matcher1 = Device.DISCUSS_AND_FIRM_PATTERN.matcher(matcher.group(1));
        while (matcher1.find()) {
            Device.PostItem item = new Device.PostItem();
            item.setId(Integer.parseInt(matcher1.group(1)));
            item.setTitle(ApiUtils.fromHtml(matcher1.group(2)));
            item.setDate(matcher1.group(3));
            if (matcher1.group(4) != null) {
                item.setDesc(ApiUtils.fromHtml(matcher1.group(4)));
            }
            data.addFirmware(item);
        }
    }
    return data;
}
Also used : Matcher(java.util.regex.Matcher) Device(forpdateam.ru.forpda.api.devdb.models.Device) NetworkResponse(forpdateam.ru.forpda.api.NetworkResponse) ArrayList(java.util.ArrayList) Pair(android.util.Pair)

Example 2 with Device

use of forpdateam.ru.forpda.api.devdb.models.Device in project ForPDA by RadiationX.

the class DeviceFragment method loadData.

@Override
public boolean loadData() {
    if (!super.loadData()) {
        return false;
    }
    refreshToolbarMenuItems(false);
    progressBar.setVisibility(View.VISIBLE);
    subscribe(RxApi.DevDb().getDevice(deviceId), this::onLoad, new Device());
    return true;
}
Also used : Device(forpdateam.ru.forpda.api.devdb.models.Device)

Aggregations

Device (forpdateam.ru.forpda.api.devdb.models.Device)2 Pair (android.util.Pair)1 NetworkResponse (forpdateam.ru.forpda.api.NetworkResponse)1 ArrayList (java.util.ArrayList)1 Matcher (java.util.regex.Matcher)1