Search in sources :

Example 11 with NetworkResponse

use of forpdateam.ru.forpda.api.NetworkResponse in project ForPDA by RadiationX.

the class Auth method getForm.

public AuthForm getForm() throws Exception {
    NetworkResponse response = Api.getWebClient().get(AUTH_BASE_URL);
    if (response.getBody() == null || response.getBody().isEmpty())
        throw new Exception("Page empty!");
    if (checkLogin(response.getBody()))
        throw new Exception("You already logged");
    AuthForm form = new AuthForm();
    Matcher matcher = captchaPattern.matcher(response.getBody());
    if (matcher.find()) {
        form.setCaptchaTime(matcher.group(1));
        form.setCaptchaSig(matcher.group(2));
        form.setCaptchaImageUrl(matcher.group(3));
    } else {
        throw new Exception("Form Not Found");
    }
    form.setBody(response.getBody());
    return form;
}
Also used : Matcher(java.util.regex.Matcher) NetworkResponse(forpdateam.ru.forpda.api.NetworkResponse) AuthForm(forpdateam.ru.forpda.api.auth.models.AuthForm)

Example 12 with NetworkResponse

use of forpdateam.ru.forpda.api.NetworkResponse in project ForPDA by RadiationX.

the class DevDb method search.

public DeviceSearch search(String query) throws Exception {
    DeviceSearch searchResult = new DeviceSearch();
    try {
        query = URLDecoder.decode(query, "windows-1251");
    } catch (Exception ignore) {
    }
    NetworkResponse response = Api.getWebClient().get("http://4pda.ru/devdb/search?s=" + query);
    Matcher matcher = SEARCH_PATTERN.matcher(response.getBody());
    while (matcher.find()) {
        DeviceSearch.DeviceItem item = new DeviceSearch.DeviceItem();
        item.setImageSrc(matcher.group(1));
        item.setId(matcher.group(2));
        item.setTitle(ApiUtils.fromHtml(matcher.group(3)));
        searchResult.addDevice(item);
    }
    searchResult.setAll(searchResult.getDevices().size());
    searchResult.setActual(searchResult.getAll());
    return searchResult;
}
Also used : DeviceSearch(forpdateam.ru.forpda.api.devdb.models.DeviceSearch) Matcher(java.util.regex.Matcher) NetworkResponse(forpdateam.ru.forpda.api.NetworkResponse)

Example 13 with NetworkResponse

use of forpdateam.ru.forpda.api.NetworkResponse 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 14 with NetworkResponse

use of forpdateam.ru.forpda.api.NetworkResponse in project ForPDA by RadiationX.

the class Client method request.

public NetworkResponse request(NetworkRequest request, OkHttpClient client, ProgressListener uploadProgressListener) throws Exception {
    Request.Builder requestBuilder = prepareRequest(request, uploadProgressListener);
    NetworkResponse response = new NetworkResponse(request.getUrl());
    Response okHttpResponse = null;
    try {
        okHttpResponse = client.newCall(requestBuilder.build()).execute();
        if (!okHttpResponse.isSuccessful()) {
            if (okHttpResponse.code() == 403) {
                String content = okHttpResponse.body().string();
                // forpdateam.ru.forpda.utils.ApiUtils.longLog(content);
                new Handler(Looper.getMainLooper()).post(() -> {
                    try {
                        if (TabManager.get().getTagContainClass(GoogleCaptchaFragment.class) == null) {
                            Bundle args = new Bundle();
                            args.putString("content", content);
                            TabManager.get().add(GoogleCaptchaFragment.class, args);
                        }
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                });
            }
            throw new OkHttpResponseException(okHttpResponse.code(), okHttpResponse.message(), request.getUrl());
        }
        response.setCode(okHttpResponse.code());
        response.setMessage(okHttpResponse.message());
        response.setRedirect(okHttpResponse.request().url().toString());
        if (!request.isWithoutBody()) {
            response.setBody(okHttpResponse.body().string());
            getCounts(response.getBody());
            checkForumErrors(response.getBody());
        }
        Log.d(LOG_TAG, "Response: " + response.toString());
    } finally {
        if (okHttpResponse != null)
            okHttpResponse.close();
    }
    return response;
}
Also used : Response(okhttp3.Response) NetworkResponse(forpdateam.ru.forpda.api.NetworkResponse) Bundle(android.os.Bundle) NetworkRequest(forpdateam.ru.forpda.api.NetworkRequest) Request(okhttp3.Request) NetworkResponse(forpdateam.ru.forpda.api.NetworkResponse) Handler(android.os.Handler) GoogleCaptchaFragment(forpdateam.ru.forpda.ui.fragments.other.GoogleCaptchaFragment) GeneralSecurityException(java.security.GeneralSecurityException)

Example 15 with NetworkResponse

use of forpdateam.ru.forpda.api.NetworkResponse in project ForPDA by RadiationX.

the class Search method getSearch.

public SearchResult getSearch(SearchSettings settings) throws Exception {
    SearchResult result = new SearchResult();
    NetworkResponse response = Api.getWebClient().get(settings.toUrl());
    Matcher matcher = null;
    SearchItem item = null;
    boolean isNews = settings.getResourceType().equals(SearchSettings.RESOURCE_NEWS.first);
    boolean resultTopics = settings.getResult().equals(SearchSettings.RESULT_TOPICS.first);
    if (isNews) {
        matcher = newsListPattern.matcher(response.getBody());
        while (matcher.find()) {
            item = new SearchItem();
            item.setId(Integer.parseInt(matcher.group(1)));
            item.setImageUrl(matcher.group(2));
            item.setDate(matcher.group(3));
            item.setUserId(Integer.parseInt(matcher.group(4)));
            item.setNick(ApiUtils.fromHtml(matcher.group(5)));
            item.setTitle(ApiUtils.fromHtml(matcher.group(6)));
            item.setBody(matcher.group(7));
            result.addItem(item);
        }
    } else {
        if (resultTopics) {
            matcher = forumTopicsPattern.matcher(response.getBody());
            while (matcher.find()) {
                item = new SearchItem();
                item.setTopicId(Integer.parseInt(matcher.group(1)));
                // item.setId(Integer.parseInt(matcher.group(1)));
                item.setTitle(ApiUtils.fromHtml(matcher.group(4)));
                item.setDesc(ApiUtils.fromHtml(matcher.group(5)));
                item.setForumId(Integer.parseInt(matcher.group(6)));
                item.setUserId(Integer.parseInt(matcher.group(10)));
                item.setNick(ApiUtils.fromHtml(matcher.group(11)));
                item.setDate(matcher.group(12));
                result.addItem(item);
            }
        } else {
            matcher = universalForumPosts.matcher(response.getBody());
            while (matcher.find()) {
                item = new SearchItem();
                item.setTopicId(Integer.parseInt(matcher.group(2)));
                item.setId(Integer.parseInt(matcher.group(3)));
                item.setTitle(ApiUtils.fromHtml(matcher.group(4)));
                item.setDate(matcher.group(5));
                // item.setNumber(Integer.parseInt(matcher.group(6)));
                item.setOnline(matcher.group(7).contains("green"));
                String avatar = matcher.group(8);
                if (!avatar.isEmpty()) {
                    avatar = "https://s.4pda.to/forum/uploads/".concat(avatar);
                }
                item.setAvatar(avatar);
                item.setNick(ApiUtils.fromHtml(matcher.group(9)));
                item.setUserId(Integer.parseInt(matcher.group(10)));
                item.setCurator(matcher.group(11) != null);
                item.setGroupColor(matcher.group(12));
                item.setGroup(matcher.group(13));
                item.setCanMinus(!matcher.group(14).isEmpty());
                item.setReputation(matcher.group(15));
                item.setCanPlus(!matcher.group(16).isEmpty());
                item.setCanReport(!matcher.group(17).isEmpty());
                item.setCanEdit(!matcher.group(18).isEmpty());
                item.setCanDelete(!matcher.group(19).isEmpty());
                item.setCanQuote(!matcher.group(20).isEmpty());
                item.setBody(matcher.group(21));
                result.addItem(item);
            }
        }
    }
    if (isNews) {
        result.setPagination(Pagination.parseNews(response.getBody()));
    } else {
        result.setPagination(Pagination.parseForum(response.getBody()));
    }
    result.setSettings(settings);
    return result;
}
Also used : Matcher(java.util.regex.Matcher) NetworkResponse(forpdateam.ru.forpda.api.NetworkResponse) SearchResult(forpdateam.ru.forpda.api.search.models.SearchResult) SearchItem(forpdateam.ru.forpda.api.search.models.SearchItem)

Aggregations

NetworkResponse (forpdateam.ru.forpda.api.NetworkResponse)44 Matcher (java.util.regex.Matcher)27 NetworkRequest (forpdateam.ru.forpda.api.NetworkRequest)25 ArrayList (java.util.ArrayList)8 AttachmentItem (forpdateam.ru.forpda.api.theme.editpost.models.AttachmentItem)6 Bundle (android.os.Bundle)3 RequestFile (forpdateam.ru.forpda.api.RequestFile)3 Context (android.content.Context)2 Uri (android.net.Uri)2 App (forpdateam.ru.forpda.App)2 R (forpdateam.ru.forpda.R)2 QmsMessage (forpdateam.ru.forpda.api.qms.models.QmsMessage)2 Client (forpdateam.ru.forpda.client.Client)2 Observable (io.reactivex.Observable)2 AndroidSchedulers (io.reactivex.android.schedulers.AndroidSchedulers)2 Schedulers (io.reactivex.schedulers.Schedulers)2 Activity (android.app.Activity)1 DownloadManager (android.app.DownloadManager)1 ActivityNotFoundException (android.content.ActivityNotFoundException)1 Intent (android.content.Intent)1