-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy patholdsearch.java
384 lines (322 loc) · 15.2 KB
/
oldsearch.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
package com.powerpoint45.dtube;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
/**
* Created by michael on 23/11/17.
*/
public class SearchActivity extends AppCompatActivity {
final static String SEARCH_URL = "https://search.esteem.app/api/search";
EditText searchBar;
RecyclerView recyclerView;
ImageView askSteemLogo;
LinearLayoutManager layoutManager;
LayoutInflater inflater;
VideoArrayList videos;
ChannelAdapter adapter;
boolean hasMorePages;
boolean gettingVideos;
String querry;
int pageNumber = 0;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Preferences.darkMode)
setTheme(R.style.AppThemeDark);
setContentView(R.layout.activity_search);
recyclerView = findViewById(R.id.search_list);
askSteemLogo = findViewById(R.id.search_logo_asksteem);
// if (Preferences.darkMode)
// askSteemLogo.setImageResource(R.drawable.asksteemwhite);
inflater = getLayoutInflater();
videos = new VideoArrayList();
layoutManager = new LinearLayoutManager(this);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(layoutManager);
adapter = new ChannelAdapter(videos,this);
recyclerView.setAdapter(adapter);
Toolbar tb = findViewById(R.id.search_toolbar);
setSupportActionBar(tb);
Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
searchBar = findViewById(R.id.searchView);
tb.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
searchBar.postDelayed(new Runnable() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
searchBar.requestFocus();
InputMethodManager mgr = (InputMethodManager)SearchActivity.this.getSystemService(
Context.INPUT_METHOD_SERVICE);
assert mgr != null;
mgr.showSoftInput(searchBar, InputMethodManager.SHOW_IMPLICIT);
}
});
}
},100);
searchBar.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
if (searchBar.getText().toString().length()>0) {
findViewById(R.id.search_progress).setVisibility(View.VISIBLE);
esteemSearch(searchBar.getText().toString());
InputMethodManager mgr = (InputMethodManager)SearchActivity.this.getSystemService(
Context.INPUT_METHOD_SERVICE);
assert mgr != null;
mgr.hideSoftInputFromWindow(searchBar.getWindowToken(), 0);
}
return false;
}
return false;
}
});
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (hasMorePages && !gettingVideos) {
if (videos!=null && videos.size() - layoutManager.findLastVisibleItemPosition() < 5) {
esteemSearch(querry);
}
}
}
});
}
protected void esteemSearch(final String q) {
if (q!=null && querry!=null && !q.equals(querry)) {
videos.clear();
adapter.notifyDataSetChanged();
pageNumber = 0;
}
querry = q;
Thread t = new Thread() {
public void run() {
gettingVideos = true;
pageNumber++;
Log.d("dtube", "Loading Page "+pageNumber);
Looper.prepare(); //For Preparing Message Pool for the childThread
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(SEARCH_URL);
try {
// Request parameters and other properties.
List<NameValuePair> params = new ArrayList<>(3);
params.add(new BasicNameValuePair("q", q+"\"▶️ DTube\""));
params.add(new BasicNameValuePair("so", "newest"));
//in the future search method can be an option
//params.add(new BasicNameValuePair("so", "popularity"));
params.add(new BasicNameValuePair("pa", pageNumber+""));
httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
//Execute and get the response.
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
if (entity != null) {
try (InputStream in = entity.getContent()) {
InputStreamReader cin = new InputStreamReader(in);
StringBuilder buffer = new StringBuilder();
int c;
do {
c = cin.read();
if (c!=-1) {
buffer.append((char) c);
}
} while(c !=-1);
JSONObject resultsObj = new JSONObject(buffer.toString());
if (resultsObj.has("pages")){
hasMorePages = pageNumber < resultsObj.getInt("pages");
}
if (resultsObj.has("results")){
JSONArray resultsArr = resultsObj.getJSONArray("results");
for (int i = 0; i<resultsArr.length(); i++) {
JSONObject videoObject = resultsArr.getJSONObject(i);
if (!videoObject.toString().contains("nsfw")) {
if (videoObject.toString().contains("dtube\\/0.")
|| videoObject.toString().contains("oneloveipfs")) {
Video v = new Video();
v.title = videoObject.getString("title");
v.user = videoObject.getString("author");
v.setTime(videoObject.getString("created_at"));
v.permlink = videoObject.getString("permlink");
String body = videoObject.getString("body");
//Extract img Hash from HTML such as <img src='https://ipfs.io/ipfs/QmQG6gPe6hnT8aTRvH3hskMiWbbn9HR6gVf2TH3vXYV71Q' >
int imgTagIndex = body.indexOf("<img src='");
if (imgTagIndex != -1) {
String imageURL = body.substring(imgTagIndex+10,body.indexOf(">", imgTagIndex)-1);
if (imageURL.endsWith("'"))
imageURL = imageURL.substring(0,imageURL.length()-1);
v.setImageURL(imageURL);
}else {
Log.d("dtube", "invalid snap. Replacing img");
imgTagIndex = body.indexOf("(https://cdn.steemitimages.com/");
String imgURL = body.substring(imgTagIndex+1, body.indexOf(".png",imgTagIndex)+4);
v.setImageURL(imgURL);
}
videos.add(v);
Log.d("dtube", v.user+","+v.snapHash);
}
}
}
}
SearchActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
if (videos.size()>0)
askSteemLogo.setVisibility(View.GONE);
else
askSteemLogo.setVisibility(View.VISIBLE);
adapter.setVideos(videos);
findViewById(R.id.search_progress).setVisibility(View.GONE);
}
});
}
}
}catch (Exception e){
e.printStackTrace();
Log.d("dtube",e.getMessage());
SearchActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
findViewById(R.id.search_progress).setVisibility(View.GONE);
}
});
}
finally {
gettingVideos = false;
}
Looper.loop(); //Loop in the message queue
}
};
t.start();
}
// public void goSearch(String q){
// if (q!=null && querry!=null && !q.equals(querry)) {
// videos.clear();
// adapter.notifyDataSetChanged();
// pageNumber = 0;
// }
// querry = q;
// class searchRunner implements Runnable{
// private String q;
// private searchRunner(String q){
// this.q = q;
// }
//
// @Override
// public void run() {
// Log.d("dtube5","to search for "+q);
// BufferedReader reader = null;
// try {
// gettingVideos = true;
// pageNumber++;
// URL url = new URL(SEARCH_URL+q.replace(" ","%20")+"&pg="+pageNumber);
// reader = new BufferedReader(new InputStreamReader(url.openStream()));
// StringBuilder buffer = new StringBuilder();
// int read;
// char[] chars = new char[1024];
// while ((read = reader.read(chars)) != -1)
// buffer.append(chars, 0, read);
//
// JSONObject resultsObj = new JSONObject(buffer.toString());
// if (resultsObj.has("pages")){
// hasMorePages = resultsObj.getJSONObject("pages").getBoolean("has_next");
// Log.d("dtube5","more:"+hasMorePages);
// }
// if (resultsObj.has("results")){
// JSONArray resultsArr = resultsObj.getJSONArray("results");
// for (int i = 0; i<resultsArr.length(); i++){
// JSONObject videoObject = resultsArr.getJSONObject(i);
// if (!videoObject.toString().contains("nsfw")) {
// if (videoObject.has("meta")) {
//
// Video v = new Video();
// v.title = videoObject.getString("title");
// v.user = videoObject.getString("author");
// v.setTime(videoObject.getString("created"));
// v.permlink = videoObject.getString("permlink");
// JSONObject videoMeta = new JSONObject(videoObject.getString("meta"));
//
// if (videoMeta.has("video")) {
// v.snapHash = videoMeta.getJSONObject("video").getJSONObject("info").getString("snaphash");
//
// videos.add(v);
// }
// }
// }
//
// }
//
// SearchActivity.this.runOnUiThread(new Runnable() {
// @Override
// public void run() {
// if (videos.size()>0)
// askSteemLogo.setVisibility(View.GONE);
// else
// askSteemLogo.setVisibility(View.VISIBLE);
// adapter.setVideos(videos);
// }
// });
// }
//
// }catch (Exception e){
// e.printStackTrace();
// }finally {
// gettingVideos = false;
// if (reader!=null)
// try {
// reader.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
// }
// };
// new Thread(new searchRunner(q)).start();
// }
public void onItemClick(int pos){
Intent data = new Intent();
Bundle b = new Bundle();
b.putSerializable("video", videos.get(pos));
data.putExtra("video",b);
setResult(RESULT_OK, data);
finish();
}
public void subscribeButtonClicked(View v){
}
}