Added README.txt and GPL Header to Source Files
[DHBWCampusApp.git] / app / src / main / java / de / dhbwloe / campusapp / search / DhbwSearchHelper.java
1 /* DhbwSearchHelper.java
2  *
3  * This program is free software: you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation, either version 3 of the License, or
6  * (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 package de.dhbwloe.campusapp.search;
17
18 import android.util.Log;
19
20 import com.loopj.android.http.AsyncHttpClient;
21 import com.loopj.android.http.AsyncHttpResponseHandler;
22 import com.loopj.android.http.RequestParams;
23
24 import org.jsoup.nodes.Document;
25 import org.jsoup.nodes.Element;
26 import org.jsoup.select.Elements;
27
28 import java.io.ByteArrayInputStream;
29 import java.io.InputStream;
30 import java.io.UnsupportedEncodingException;
31 import java.net.URLEncoder;
32 import java.util.ArrayList;
33 import java.util.List;
34
35 import cz.msebera.android.httpclient.Header;
36 import de.dhbwloe.campusapp.fragments.AppSearchListItem;
37
38 /**
39  * Created by pk910 on 25.01.2016.
40  */
41 public class DhbwSearchHelper extends SearchHelper {
42
43     public DhbwSearchHelper() {
44         super(true);
45     }
46
47     @Override
48     protected void processWebRequest(AsyncHttpResponseHandler handler, String keywords) {
49         AsyncHttpClient client = new AsyncHttpClient();
50         String url = "https://www.dhbw-loerrach.de/suche.html?&L=0";
51         RequestParams params = new RequestParams();
52         /*  WHAT THE HELL DO THEY REQUIRE FOR THEIR STUPID SEARCH!?
53         tx_indexedsearch[_sections]:0
54         tx_indexedsearch[_freeIndexUid]:_
55         tx_indexedsearch[pointer]:0
56         tx_indexedsearch[ext]:1
57         tx_indexedsearch[defOp]:0
58         tx_indexedsearch[sword]:paintball
59         tx_indexedsearch[type]:1
60         tx_indexedsearch[defOp]:0
61         tx_indexedsearch[media]:-1
62         tx_indexedsearch[lang]:-1
63         tx_indexedsearch[sections]:0
64         tx_indexedsearch[order]:rank_flag
65         tx_indexedsearch[desc]:0
66         tx_indexedsearch[results]:10
67         tx_indexedsearch[group]:flat
68         tx_indexedsearch[extResume]:0
69         tx_indexedsearch[extResume]:1
70         tx_indexedsearch[submit_button]:Suche
71          */
72         params.put("tx_indexedsearch[_sections]","0");
73         params.put("tx_indexedsearch[_freeIndexUid]","_");
74         params.put("tx_indexedsearch[pointer]","0");
75         params.put("tx_indexedsearch[ext]","1");
76         params.put("tx_indexedsearch[defOp]","0");
77         params.put("tx_indexedsearch[sword]", keywords);
78         params.put("tx_indexedsearch[type]","1");
79         params.put("tx_indexedsearch[defOp]","0");
80         params.put("tx_indexedsearch[media]","-1");
81         params.put("tx_indexedsearch[lang]","-1");
82         params.put("tx_indexedsearch[sections]","0");
83         params.put("tx_indexedsearch[order]","rank_flag");
84         params.put("tx_indexedsearch[desc]","0");
85         params.put("tx_indexedsearch[results]","40");
86         params.put("tx_indexedsearch[group]","flat");
87         params.put("tx_indexedsearch[extResume]","0");
88         params.put("tx_indexedsearch[extResume]","1");
89         params.put("tx_indexedsearch[submit_button]","Suche");
90
91         Log.i("DhbwSearchHelper", "Search: "+keywords+" ("+url+")");
92         client.post(url, params, handler);
93     }
94
95     @Override
96     protected List<AppSearchListItem> onHtmlDocumentReceived(Document document) {
97         ArrayList<AppSearchListItem> results = new ArrayList<AppSearchListItem>();
98         Elements searchResultsTables = document.select(".tx-indexedsearch-res > table");
99         for(Element searchResultsTable : searchResultsTables) {
100             SearchTarget target = new SearchTarget();
101             target.setInAppTarget(true);
102             target.setTargetUrl("WebBrowser");
103
104             Elements titleLinkElement = searchResultsTable.select(".tx-indexedsearch-title > a");
105             Elements descriptionElement = searchResultsTable.select(".tx-indexedsearch-descr");
106             if(titleLinkElement.size() == 0 || titleLinkElement.size() == 0)
107                 continue;
108             String title = titleLinkElement.get(0).text();
109             String link = titleLinkElement.get(0).attr("href");
110             if(!link.matches("^https?://.*"))
111                 link = "https://www.dhbw-loerrach.de/" + link;
112
113             String description = descriptionElement.get(0).text();
114
115             target.setArgument("url", link);
116
117             AppSearchListItem result = new AppSearchListItem("DHBW: "+title, description, target);
118             results.add(result);
119         }
120         return results;
121     }
122
123     @Override
124     protected List<AppSearchListItem> onPlainTextReceived(byte[] data) {
125         return null;
126     }
127 }