Skip to content

Commit

Permalink
refactor(player): remove usage of lodash
Browse files Browse the repository at this point in the history
  • Loading branch information
davinkevin committed Jan 27, 2016
1 parent 266c541 commit 572eb2d
Show file tree
Hide file tree
Showing 19 changed files with 322 additions and 214 deletions.
5 changes: 2 additions & 3 deletions src/main/java/lan/dk/podcastserver/config/BeanConfigScan.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@
* Created by kevin on 26/12/2013.
*/
@Configuration
@ComponentScan(basePackages = { "lan.dk.podcastserver.utils",
"lan.dk.podcastserver.service",
"lan.dk.podcastserver.business"})
@ComponentScan(basePackages = { "lan.dk.podcastserver.utils", "lan.dk.podcastserver.service", "lan.dk.podcastserver.business"})
public class BeanConfigScan {

@Bean(name="Validator")
public LocalValidatorFactoryBean validator() {
return new LocalValidatorFactoryBean();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
import lan.dk.podcastserver.entity.Item;
import lan.dk.podcastserver.manager.ItemDownloadManager;
import lan.dk.podcastserver.service.UrlService;
import lan.dk.podcastserver.service.factory.WGetHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.net.URL;
import java.util.Objects;

import static java.util.Objects.isNull;
Expand All @@ -25,6 +25,7 @@
public class HTTPDownloader extends AbstractDownloader {

@Autowired UrlService urlService;
@Autowired WGetHelper wGetHelper;

DownloadInfo info = null;

Expand All @@ -36,15 +37,15 @@ public Item download() {
//int borne = randomGenerator.nextInt(100);
try {
// initialize url information object
info = new DownloadInfo(new URL(urlService.getRealURL(getItemUrl())));
info = wGetHelper.wDownloadInfo(urlService.getRealURL(getItemUrl()));
// extract infromation from the web
Runnable itemSynchronisation = new HTTPWatcher(this);

info.extract(stopDownloading, itemSynchronisation);
target = getTagetFile(this.item);
target = getTagetFile(item);

// create wget downloader
WGet w = new WGet(info, target);
WGet w = wGetHelper.wGet(info, target);
// will blocks until download finishes
w.download(stopDownloading, itemSynchronisation);
} catch (DownloadMultipartError e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import lan.dk.podcastserver.entity.Item;
import lan.dk.podcastserver.entity.Status;
import lan.dk.podcastserver.service.UrlService;
import lan.dk.podcastserver.utils.URLUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -70,7 +69,7 @@ public File getTagetFile (Item item) {
if (target != null)
return target;

String fileNameFromM3U8Playlist = URLUtils.getFileNameM3U8Url(item.getUrl());
String fileNameFromM3U8Playlist = urlService.getFileNameM3U8Url(item.getUrl());

File finalFile = new File(itemDownloadManager.getRootfolder() + File.separator + item.getPodcast().getTitle() + File.separator + fileNameFromM3U8Playlist);
logger.debug("Création du fichier : {}", finalFile.getAbsolutePath());
Expand Down Expand Up @@ -139,15 +138,10 @@ public void run() {
}

if (downloader.stopDownloading.get()) {
if (Status.STOPPED == item.getStatus()) {
break;
}

if (Status.PAUSED == item.getStatus()) {
synchronized(this) {
wait();
}
}
if (Status.STOPPED == item.getStatus()) break;

if (Status.PAUSED == item.getStatus())
synchronized(this) { wait(); }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
import lan.dk.podcastserver.entity.Item;
import lan.dk.podcastserver.service.FfmpegService;
import lan.dk.podcastserver.utils.URLUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

Expand All @@ -25,6 +24,9 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static java.util.Objects.isNull;
import static java.util.Objects.nonNull;

/**
* Created by kevin on 13/07/2014.
*/
Expand Down Expand Up @@ -228,56 +230,49 @@ public File getTagetFile (String url) {
return new File(finalFile.getAbsolutePath() + temporaryExtension) ;
}

@Slf4j
static class ParleysWatcher implements Runnable {

protected final Logger logger = LoggerFactory.getLogger(this.getClass());
private final ParleysDownloader parleysDownloader;
private final DownloadInfo info;
private final Item item;
private final Long totalSize;
private long last;
private Integer avancementIntermediaire;
private Integer avancementIntermediaire = 0;

public ParleysWatcher(ParleysDownloader parleysDownloader) {
this.parleysDownloader = parleysDownloader;
this.info = parleysDownloader.info;
this.item = parleysDownloader.item;
this.totalSize = parleysDownloader.totalSize;
this.avancementIntermediaire = 0;
}

@Override
public void run() {
switch (info.getState()) {
Item item = parleysDownloader.item;
DownloadInfo info = parleysDownloader.info;

switch (info.getState()) {
case EXTRACTING:
case EXTRACTING_DONE:
logger.debug(FilenameUtils.getName(String.valueOf(item.getUrl())) + " " + info.getState());
log.debug(FilenameUtils.getName(String.valueOf(item.getUrl())) + " " + info.getState());
break;
case ERROR:
parleysDownloader.stopDownload();
break;
case DONE:
logger.debug(FilenameUtils.getName(String.valueOf(item.getUrl())) + " - terminé");
log.debug(FilenameUtils.getName(String.valueOf(item.getUrl())) + " - terminé");
avancementIntermediaire = item.getProgression();
break;
case RETRYING:
logger.debug(FilenameUtils.getName(String.valueOf(item.getUrl())) + " " + info.getState() + " " + info.getDelay());
log.debug(FilenameUtils.getName(String.valueOf(item.getUrl())) + " " + info.getState() + " " + info.getDelay());
break;
case DOWNLOADING:
long now = System.currentTimeMillis();

if (now - 1000 > last && info.getLength() != null && info.getLength() != 0L) {
last = now;
int progression = ((int) (info.getCount()*100 / (float) totalSize))+avancementIntermediaire;
if (item.getProgression() < progression) {
item.setProgression(progression);
logger.debug("Progression de {} : {}%", item.getTitle(), progression);
parleysDownloader.convertAndSaveBroadcast();
}
if (isNull(info.getLength()) || (nonNull(info.getLength()) && info.getLength() != 0L)) break;

int progression = ((int) (info.getCount()*100 / (float) parleysDownloader.totalSize))+avancementIntermediaire;
if (item.getProgression() < progression) {
item.setProgression(progression);
log.debug("Progression de {} : {}%", item.getTitle(), progression);
parleysDownloader.convertAndSaveBroadcast();
}

break;
case STOP:
logger.debug("Pause / Arrêt du téléchargement du téléchargement");
log.debug("Pause / Arrêt du téléchargement du téléchargement");
break;
default:
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import lan.dk.podcastserver.entity.Item;
import lan.dk.podcastserver.entity.Status;
import lan.dk.podcastserver.service.factory.ProcessBuilderFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
Expand All @@ -17,6 +19,8 @@ public class RTMPDownloader extends AbstractDownloader {

private int pid = 0;

@Autowired ProcessBuilderFactory processBuilderFactory;

@Value("${podcastserver.externaltools.rtmpdump:/usr/local/bin/rtmpdump}")
String rtmpdump;

Expand All @@ -30,15 +34,13 @@ public Item download() {
if (item.getUrl().contains("rtmp://")) {
try {
target = getTagetFile(item);
ProcessBuilder pb = new ProcessBuilder(rtmpdump,
"-r",
item.getUrl(),
"-o",
target.getAbsolutePath());

pb.directory(new File("/tmp"));
logger.debug("Fichier de sortie : " + target.getAbsolutePath());

ProcessBuilder pb = processBuilderFactory
.newProcessBuilder(rtmpdump, "-r", getItemUrl(), "-o", target.getAbsolutePath())
.directory(new File("/tmp"));


p = pb.start();
if (p.getClass().getSimpleName().contains("UNIXProcess")) {
Field pidField = p.getClass().getDeclaredField("pid");
Expand All @@ -55,12 +57,12 @@ private BufferedReader getBufferedReader(InputStream is) {
@Override
public void run() {
BufferedReader br = getBufferedReader(p.getErrorStream());
String ligne = "";
String ligne;
Pattern p = Pattern.compile("[^\\(]*\\(([0-9]*).*%\\)");
Matcher m = null;
Matcher m;
logger.debug("Lecture du stream d'erreur");
try {
long time = System.currentTimeMillis();

while ((ligne = br.readLine()) != null) {
//logger.debug(ligne);
m = p.matcher(ligne);
Expand Down
Loading

0 comments on commit 572eb2d

Please sign in to comment.