1
0
mirror of https://github.com/TeamNewPipe/NewPipe synced 2024-06-25 22:53:20 +00:00

Merge pull request #322 from DevFactory/release/string-literals-should-not-be-duplicated-fix-1

Code quality fix - String literals should not be duplicated.
This commit is contained in:
Christian Schabesberger 2016-06-30 16:56:41 +02:00 committed by GitHub
commit faed0958b3
6 changed files with 38 additions and 29 deletions

View File

@ -33,6 +33,7 @@ import java.io.IOException;
*/
public class YoutubeStreamExtractorDefaultTest extends AndroidTestCase {
public static final String HTTPS = "https://";
private StreamExtractor extractor;
public void setUp() throws IOException, ExtractionException {
@ -80,12 +81,12 @@ public class YoutubeStreamExtractorDefaultTest extends AndroidTestCase {
public void testGetThumbnailUrl() throws ParsingException {
assertTrue(extractor.getThumbnailUrl(),
extractor.getThumbnailUrl().contains("https://"));
extractor.getThumbnailUrl().contains(HTTPS));
}
public void testGetUploaderThumbnailUrl() throws ParsingException {
assertTrue(extractor.getUploaderThumbnailUrl(),
extractor.getUploaderThumbnailUrl().contains("https://"));
extractor.getUploaderThumbnailUrl().contains(HTTPS));
}
public void testGetAudioStreams() throws ParsingException {
@ -95,7 +96,7 @@ public class YoutubeStreamExtractorDefaultTest extends AndroidTestCase {
public void testGetVideoStreams() throws ParsingException {
for(VideoStream s : extractor.getVideoStreams()) {
assertTrue(s.url,
s.url.contains("https://"));
s.url.contains(HTTPS));
assertTrue(s.resolution.length() > 0);
assertTrue(Integer.toString(s.format),
0 <= s.format && s.format <= 4);

View File

@ -12,6 +12,7 @@ import org.schabi.newpipe.extractor.VideoStream;
import java.io.IOException;
public class YoutubeStreamExtractorRestrictedTest extends AndroidTestCase {
public static final String HTTPS = "https://";
private StreamExtractor extractor;
public void setUp() throws IOException, ExtractionException {
@ -63,12 +64,12 @@ public class YoutubeStreamExtractorRestrictedTest extends AndroidTestCase {
public void testGetThumbnailUrl() throws ParsingException {
assertTrue(extractor.getThumbnailUrl(),
extractor.getThumbnailUrl().contains("https://"));
extractor.getThumbnailUrl().contains(HTTPS));
}
public void testGetUploaderThumbnailUrl() throws ParsingException {
assertTrue(extractor.getUploaderThumbnailUrl(),
extractor.getUploaderThumbnailUrl().contains("https://"));
extractor.getUploaderThumbnailUrl().contains(HTTPS));
}
public void testGetAudioStreams() throws ParsingException {
@ -78,7 +79,7 @@ public class YoutubeStreamExtractorRestrictedTest extends AndroidTestCase {
public void testGetVideoStreams() throws ParsingException {
for(VideoStream s : extractor.getVideoStreams()) {
assertTrue(s.url,
s.url.contains("https://"));
s.url.contains(HTTPS));
assertTrue(s.resolution.length() > 0);
assertTrue(Integer.toString(s.format),
0 <= s.format && s.format <= 4);

View File

@ -85,6 +85,7 @@ public class VideoItemListFragment extends ListFragment {
}
private class SearchRunnable implements Runnable {
public static final String YOUTUBE = "Youtube";
private final SearchEngine engine;
private final String query;
private final int page;
@ -129,7 +130,7 @@ public class VideoItemListFragment extends ListFragment {
View rootView = a.findViewById(R.id.videoitem_list);
ErrorActivity.reportError(h, getActivity(), result.errors, null, rootView,
ErrorActivity.ErrorInfo.make(ErrorActivity.SEARCHED,
/* todo: this shoudl not be assigned static */ "Youtube", query, R.string.light_parsing_error));
/* todo: this shoudl not be assigned static */ YOUTUBE, query, R.string.light_parsing_error));
}
// hard errors:
@ -142,14 +143,14 @@ public class VideoItemListFragment extends ListFragment {
ErrorActivity.reportError(h, getActivity(), e, null, null,
ErrorActivity.ErrorInfo.make(ErrorActivity.SEARCHED,
/* todo: this shoudl not be assigned static */
"Youtube", query, R.string.parsing_error));
YOUTUBE, query, R.string.parsing_error));
//postNewErrorToast(h, R.string.parsing_error);
e.printStackTrace();
} catch(Exception e) {
ErrorActivity.reportError(h, getActivity(), e, null, null,
ErrorActivity.ErrorInfo.make(ErrorActivity.SEARCHED,
/* todo: this shoudl not be assigned static */ "Youtube", query, R.string.general_error));
/* todo: this shoudl not be assigned static */ YOUTUBE, query, R.string.general_error));
e.printStackTrace();
}

View File

@ -58,6 +58,7 @@ public class MainActivity extends AppCompatActivity implements AdapterView.OnIte
public static final String INTENT_LIST = "us.shandian.giga.intent.LIST";
private static final String TAG = MainActivity.class.toString();
public static final String THREADS = "threads";
private MissionsFragment mFragment;
@ -105,7 +106,7 @@ public class MainActivity extends AppCompatActivity implements AdapterView.OnIte
actionBar.setTitle(R.string.downloads_title);
actionBar.setDisplayShowTitleEnabled(true);
mPrefs = getSharedPreferences("threads", Context.MODE_WORLD_READABLE);
mPrefs = getSharedPreferences(THREADS, Context.MODE_WORLD_READABLE);
// Fragment
getWindow().getDecorView().getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@ -179,7 +180,7 @@ public class MainActivity extends AppCompatActivity implements AdapterView.OnIte
});
int def = mPrefs.getInt("threads", 4);
int def = mPrefs.getInt(THREADS, 4);
threads.setProgress(def - 1);
tCount.setText(String.valueOf(def));
@ -222,7 +223,7 @@ public class MainActivity extends AppCompatActivity implements AdapterView.OnIte
mBinder.onMissionAdded(mManager.getMission(res));
mFragment.notifyChange();
mPrefs.edit().putInt("threads", threads.getProgress() + 1).commit();
mPrefs.edit().putInt(THREADS, threads.getProgress() + 1).commit();
mPendingUrl = null;
dialog.dismiss();
}

View File

@ -50,6 +50,7 @@ import javax.xml.parsers.ParserConfigurationException;
public class YoutubeSearchEngine extends SearchEngine {
private static final String TAG = YoutubeSearchEngine.class.toString();
public static final String CHARSET_UTF_8 = "UTF-8";
public YoutubeSearchEngine(StreamUrlIdHandler urlIdHandler, int serviceId) {
super(urlIdHandler, serviceId);
@ -72,7 +73,7 @@ public class YoutubeSearchEngine extends SearchEngine {
*/
String url = "https://www.youtube.com/results"
+ "?search_query=" + URLEncoder.encode(query, "UTF-8")
+ "?search_query=" + URLEncoder.encode(query, CHARSET_UTF_8)
+ "&page=" + Integer.toString(page)
+ "&filters=" + "video";
@ -151,8 +152,8 @@ public class YoutubeSearchEngine extends SearchEngine {
+ "?client=" + ""
+ "&output=" + "toolbar"
+ "&ds=" + "yt"
+ "&hl=" + URLEncoder.encode(contentCountry, "UTF-8")
+ "&q=" + URLEncoder.encode(query, "UTF-8");
+ "&hl=" + URLEncoder.encode(contentCountry, CHARSET_UTF_8)
+ "&q=" + URLEncoder.encode(query, CHARSET_UTF_8);
String response = dl.download(url);
@ -165,7 +166,7 @@ public class YoutubeSearchEngine extends SearchEngine {
try {
dBuilder = dbFactory.newDocumentBuilder();
doc = dBuilder.parse(new InputSource(
new ByteArrayInputStream(response.getBytes("utf-8"))));
new ByteArrayInputStream(response.getBytes(CHARSET_UTF_8))));
doc.getDocumentElement().normalize();
} catch (ParserConfigurationException | SAXException | IOException e) {
throw new ParsingException("Could not parse document.");

View File

@ -51,6 +51,10 @@ import java.util.regex.Pattern;
*/
public class YoutubeStreamExtractor extends StreamExtractor {
public static final String URL_ENCODED_FMT_STREAM_MAP = "url_encoded_fmt_stream_map";
public static final String HTTPS = "https:";
public static final String CONTENT = "content";
public static final String REGEX_INT = "[^\\d]";
// exceptions
@ -246,7 +250,7 @@ public class YoutubeStreamExtractor extends StreamExtractor {
// check if we have a live stream. We need to filter it, since its not yet supported.
if((playerArgs.has("ps") && playerArgs.get("ps").toString().equals("live"))
|| (playerArgs.get("url_encoded_fmt_stream_map").toString().isEmpty())) {
|| (playerArgs.get(URL_ENCODED_FMT_STREAM_MAP).toString().isEmpty())) {
isLiveStream = true;
}
} catch (JSONException e) {
@ -270,7 +274,7 @@ public class YoutubeStreamExtractor extends StreamExtractor {
playerUrl = ytAssets.getString("js");
if (playerUrl.startsWith("//")) {
playerUrl = "https:" + playerUrl;
playerUrl = HTTPS + playerUrl;
}
return playerUrl;
} catch (JSONException e) {
@ -294,7 +298,7 @@ public class YoutubeStreamExtractor extends StreamExtractor {
playerUrl = playerUrl.replace("\\", "").replace("\"", "");
if (playerUrl.startsWith("//")) {
playerUrl = "https:" + playerUrl;
playerUrl = HTTPS + playerUrl;
}
return playerUrl;
} catch (IOException e) {
@ -315,7 +319,7 @@ public class YoutubeStreamExtractor extends StreamExtractor {
je.printStackTrace();
System.err.println("failed to load title from JSON args; trying to extract it from HTML");
try { // fall through to fall-back
return doc.select("meta[name=title]").attr("content");
return doc.select("meta[name=title]").attr(CONTENT);
} catch (Exception e) {
throw new ParsingException("failed permanently to load title.", e);
}
@ -365,7 +369,7 @@ public class YoutubeStreamExtractor extends StreamExtractor {
@Override
public long getViewCount() throws ParsingException {
try {
String viewCountString = doc.select("meta[itemprop=interactionCount]").attr("content");
String viewCountString = doc.select("meta[itemprop=interactionCount]").attr(CONTENT);
return Long.parseLong(viewCountString);
} catch (Exception e) {//todo: find fallback method
throw new ParsingException("failed to get number of views", e);
@ -375,7 +379,7 @@ public class YoutubeStreamExtractor extends StreamExtractor {
@Override
public String getUploadDate() throws ParsingException {
try {
return doc.select("meta[itemprop=datePublished]").attr("content");
return doc.select("meta[itemprop=datePublished]").attr(CONTENT);
} catch (Exception e) {//todo: add fallback method
throw new ParsingException("failed to get upload date.", e);
}
@ -485,9 +489,9 @@ public class YoutubeStreamExtractor extends StreamExtractor {
String encodedUrlMap;
// playerArgs could be null if the video is age restricted
if (playerArgs == null) {
encodedUrlMap = videoInfoPage.get("url_encoded_fmt_stream_map");
encodedUrlMap = videoInfoPage.get(URL_ENCODED_FMT_STREAM_MAP);
} else {
encodedUrlMap = playerArgs.getString("url_encoded_fmt_stream_map");
encodedUrlMap = playerArgs.getString(URL_ENCODED_FMT_STREAM_MAP);
}
for(String url_data_str : encodedUrlMap.split(",")) {
try {
@ -592,7 +596,7 @@ public class YoutubeStreamExtractor extends StreamExtractor {
try {
return Integer.valueOf(doc.head()
.getElementsByAttributeValue("property", "og:restrictions:age")
.attr("content").replace("+", ""));
.attr(CONTENT).replace("+", ""));
} catch (Exception e) {
throw new ParsingException("Could not get age restriction");
}
@ -622,7 +626,7 @@ public class YoutubeStreamExtractor extends StreamExtractor {
//if this ckicks in our button has no content and thefore likes/dislikes are disabled
return -1;
}
return Integer.parseInt(likesString.replaceAll("[^\\d]", ""));
return Integer.parseInt(likesString.replaceAll(REGEX_INT, ""));
} catch (NumberFormatException nfe) {
throw new ParsingException(
"failed to parse likesString \"" + likesString + "\" as integers", nfe);
@ -642,7 +646,7 @@ public class YoutubeStreamExtractor extends StreamExtractor {
//if this kicks in our button has no content and therefore likes/dislikes are disabled
return -1;
}
return Integer.parseInt(dislikesString.replaceAll("[^\\d]", ""));
return Integer.parseInt(dislikesString.replaceAll(REGEX_INT, ""));
} catch(NumberFormatException nfe) {
throw new ParsingException(
"failed to parse dislikesString \"" + dislikesString + "\" as integers", nfe);
@ -737,7 +741,7 @@ public class YoutubeStreamExtractor extends StreamExtractor {
try {
return Long.parseLong(li.select("span.view-count")
.first().text().replaceAll("[^\\d]", ""));
.first().text().replaceAll(REGEX_INT, ""));
} catch (Exception e) {
//related videos sometimes have no view count
return 0;
@ -755,7 +759,7 @@ public class YoutubeStreamExtractor extends StreamExtractor {
thumbnailUrl = img.attr("data-thumb");
}
if (thumbnailUrl.startsWith("//")) {
thumbnailUrl = "https:" + thumbnailUrl;
thumbnailUrl = HTTPS + thumbnailUrl;
}
return thumbnailUrl;
}