Grundaufbau der App
[DHBWCampusApp.git] / app / src / main / java / de / dhbwloe / campusapp / search / DhbwSearchHelper.java
1 package de.dhbwloe.campusapp.search;
2
3 import android.util.Log;
4
5 import com.loopj.android.http.AsyncHttpClient;
6 import com.loopj.android.http.AsyncHttpResponseHandler;
7 import com.loopj.android.http.RequestParams;
8
9 import org.jsoup.nodes.Document;
10 import org.jsoup.nodes.Element;
11 import org.jsoup.select.Elements;
12
13 import java.io.ByteArrayInputStream;
14 import java.io.InputStream;
15 import java.io.UnsupportedEncodingException;
16 import java.net.URLEncoder;
17 import java.util.ArrayList;
18 import java.util.List;
19
20 import cz.msebera.android.httpclient.Header;
21 import de.dhbwloe.campusapp.fragments.AppSearchListItem;
22
23 /**
24  * Created by pk910 on 25.01.2016.
25  */
26 public class DhbwSearchHelper extends SearchHelper {
27
28     public DhbwSearchHelper() {
29         super(true);
30     }
31
32     @Override
33     protected void processWebRequest(AsyncHttpResponseHandler handler, String keywords) {
34         AsyncHttpClient client = new AsyncHttpClient();
35         String url = "https://www.dhbw-loerrach.de/suche.html?&L=0";
36         RequestParams params = new RequestParams();
37         /*  WHAT THE HELL DO THEY REQUIRE FOR THEIR STUPID SEARCH!?
38         tx_indexedsearch[_sections]:0
39         tx_indexedsearch[_freeIndexUid]:_
40         tx_indexedsearch[pointer]:0
41         tx_indexedsearch[ext]:1
42         tx_indexedsearch[defOp]:0
43         tx_indexedsearch[sword]:paintball
44         tx_indexedsearch[type]:1
45         tx_indexedsearch[defOp]:0
46         tx_indexedsearch[media]:-1
47         tx_indexedsearch[lang]:-1
48         tx_indexedsearch[sections]:0
49         tx_indexedsearch[order]:rank_flag
50         tx_indexedsearch[desc]:0
51         tx_indexedsearch[results]:10
52         tx_indexedsearch[group]:flat
53         tx_indexedsearch[extResume]:0
54         tx_indexedsearch[extResume]:1
55         tx_indexedsearch[submit_button]:Suche
56          */
57         params.put("tx_indexedsearch[_sections]","0");
58         params.put("tx_indexedsearch[_freeIndexUid]","_");
59         params.put("tx_indexedsearch[pointer]","0");
60         params.put("tx_indexedsearch[ext]","1");
61         params.put("tx_indexedsearch[defOp]","0");
62         params.put("tx_indexedsearch[sword]", keywords);
63         params.put("tx_indexedsearch[type]","1");
64         params.put("tx_indexedsearch[defOp]","0");
65         params.put("tx_indexedsearch[media]","-1");
66         params.put("tx_indexedsearch[lang]","-1");
67         params.put("tx_indexedsearch[sections]","0");
68         params.put("tx_indexedsearch[order]","rank_flag");
69         params.put("tx_indexedsearch[desc]","0");
70         params.put("tx_indexedsearch[results]","40");
71         params.put("tx_indexedsearch[group]","flat");
72         params.put("tx_indexedsearch[extResume]","0");
73         params.put("tx_indexedsearch[extResume]","1");
74         params.put("tx_indexedsearch[submit_button]","Suche");
75
76         Log.i("DhbwSearchHelper", "Search: "+keywords+" ("+url+")");
77         client.post(url, params, handler);
78     }
79
80     @Override
81     protected List<AppSearchListItem> onHtmlDocumentReceived(Document document) {
82         ArrayList<AppSearchListItem> results = new ArrayList<AppSearchListItem>();
83         Elements searchResultsTables = document.select(".tx-indexedsearch-res > table");
84         for(Element searchResultsTable : searchResultsTables) {
85             SearchTarget target = new SearchTarget();
86             target.setInAppTarget(true);
87             target.setTargetUrl("WebBrowser");
88
89             Elements titleLinkElement = searchResultsTable.select(".tx-indexedsearch-title > a");
90             Elements descriptionElement = searchResultsTable.select(".tx-indexedsearch-descr");
91             if(titleLinkElement.size() == 0 || titleLinkElement.size() == 0)
92                 continue;
93             String title = titleLinkElement.get(0).text();
94             String link = titleLinkElement.get(0).attr("href");
95             if(!link.matches("^https?://.*"))
96                 link = "https://www.dhbw-loerrach.de/" + link;
97
98             String description = descriptionElement.get(0).text();
99
100             target.setArgument("url", link);
101
102             AppSearchListItem result = new AppSearchListItem("DHBW: "+title, description, target);
103             results.add(result);
104         }
105         return results;
106     }
107
108     @Override
109     protected List<AppSearchListItem> onPlainTextReceived(byte[] data) {
110         return null;
111     }
112 }