File tree Expand file tree Collapse file tree 4 files changed +112
-0
lines changed
main/java/com/shirkanesi/magmaplayer/search
test/java/com/shirkanesi/magmaplayer/search Expand file tree Collapse file tree 4 files changed +112
-0
lines changed Original file line number Diff line number Diff line change 8686 <artifactId >jackson-databind</artifactId >
8787 <version >2.17.0</version >
8888 </dependency >
89+ <dependency >
90+ <groupId >org.junit.jupiter</groupId >
91+ <artifactId >junit-jupiter-api</artifactId >
92+ <version >5.10.2</version >
93+ <scope >test</scope >
94+ </dependency >
8995 </dependencies >
9096
9197</project >
Original file line number Diff line number Diff line change 1+ package com .shirkanesi .magmaplayer .search ;
2+
3+ import com .fasterxml .jackson .annotation .JsonIgnoreProperties ;
4+ import com .fasterxml .jackson .annotation .JsonProperty ;
5+ import lombok .Getter ;
6+
7+ @ Getter
8+ @ JsonIgnoreProperties (ignoreUnknown = true )
9+ public class SearchResult {
10+
11+ @ JsonProperty ("url" )
12+ private String url ;
13+
14+ @ JsonProperty ("title" )
15+ private String title ;
16+
17+ @ JsonProperty ("channel" )
18+ private String creator ;
19+
20+ }
Original file line number Diff line number Diff line change 1+ package com .shirkanesi .magmaplayer .search ;
2+
3+ import com .fasterxml .jackson .databind .ObjectMapper ;
4+
5+ import java .io .BufferedReader ;
6+ import java .io .IOException ;
7+ import java .io .InputStreamReader ;
8+ import java .util .ArrayList ;
9+ import java .util .List ;
10+
11+ public class SearchUtil {
12+
13+ private static final String SEARCH_COMMANDS = "yt-dlp \" %s:%s\" --print-json --flat-playlist --skip-download --no-warnings" ;
14+
15+ private static final ObjectMapper objectMapper = new ObjectMapper ();
16+
17+ public static List <SearchResult > search (String prefix , String query ) throws IOException {
18+ final String findInformation = String .format (SEARCH_COMMANDS , prefix , query );
19+ Process process = Runtime .getRuntime ().exec (findInformation );
20+
21+ List <SearchResult > results = new ArrayList <>();
22+ try (BufferedReader bufferedReader = new BufferedReader (new InputStreamReader (process .getInputStream ()))) {
23+ String line ;
24+ while ((line = bufferedReader .readLine ()) != null ) {
25+ results .add (objectMapper .readValue (line , SearchResult .class ));
26+ }
27+ }
28+
29+ return results ;
30+ }
31+
32+ }
Original file line number Diff line number Diff line change 1+ package com .shirkanesi .magmaplayer .search ;
2+
3+ import org .junit .jupiter .api .Test ;
4+
5+ import java .io .IOException ;
6+ import java .util .List ;
7+
8+ import static org .junit .jupiter .api .Assertions .*;
9+
10+ class SearchUtilTest {
11+
12+ @ Test
13+ void testSearchValid () {
14+ try {
15+ List <SearchResult > results = SearchUtil .search ("ytsearch10" , "music" );
16+
17+ assertEquals (10 , results .size ());
18+
19+ for (SearchResult result : results ) {
20+ assertNotNull (result );
21+ assertNotNull (result .getTitle ());
22+ assertNotNull (result .getUrl ());
23+ }
24+ } catch (IOException e ) {
25+ fail (e );
26+ }
27+ }
28+
29+ @ Test
30+ void testSearchSingle () {
31+ try {
32+ List <SearchResult > results = SearchUtil .search ("ytsearch" , "music" );
33+
34+ assertEquals (1 , results .size ());
35+
36+ assertNotNull (results .get (0 ));
37+ assertNotNull (results .get (0 ).getTitle ());
38+ assertNotNull (results .get (0 ).getUrl ());
39+ } catch (IOException e ) {
40+ fail (e );
41+ }
42+ }
43+
44+ @ Test
45+ void testSearchInvalid () {
46+ try {
47+ List <SearchResult > results = SearchUtil .search ("ytsearch10" , "hikodfgsoihsdghgoujuoihöfdgs" );
48+
49+ assertEquals (0 , results .size ());
50+ } catch (IOException e ) {
51+ fail (e );
52+ }
53+ }
54+ }
You can’t perform that action at this time.
0 commit comments