Skip to content

Commit

Permalink
Fix for multipart m3u8 stream downloads. Code cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
ColumPaget committed Mar 14, 2017
1 parent ff5f222 commit 7991cc8
Show file tree
Hide file tree
Showing 13 changed files with 174 additions and 171 deletions.
45 changes: 45 additions & 0 deletions common.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "common.h"
#include "download.h"

char *FileTypes[]={".flv",".mp3",".mp4",".mov",".wma",".m4a",".m4v",".wmv",".webm",".avi",".3gp","m3u8",NULL};
char *ItemSelectionArg=NULL;
Expand All @@ -8,10 +9,31 @@ char *Username=NULL, *Password=NULL;
char *Proxy=NULL;
char *ProgName=NULL, *CmdLine=NULL, *UserAgent=NULL;
int STREAMTimeout=300;
STREAM *StdIn=NULL;
int Flags=0;


char *BuildURL(char *RetStr, const char *Parent, const char *SubItem)
{
char *Proto=NULL, *Host=NULL, *Port=NULL, *Path=NULL;
char *BasePath=NULL;

ParseURL(Parent,&Proto,&Host,&Port,NULL,NULL,&Path,NULL);
if (StrValid(Port)) BasePath=FormatStr(BasePath, "%s://%s:%s/", Proto,Host,Port);
else BasePath=FormatStr(BasePath, "%s://%s/", Proto,Host);

//if it starts with '/' then we don't append to existing path
if (*SubItem=='/') RetStr=MCopyStr(RetStr, BasePath, SubItem, NULL);
else RetStr=MCopyStr(RetStr, BasePath, Path, "/", SubItem, NULL);

DestroyString(Proto);
DestroyString(Host);
DestroyString(Port);
DestroyString(Path);
DestroyString(BasePath);

return(RetStr);
}

char *FileTypeFromURL(char *URL)
{
Expand Down Expand Up @@ -84,3 +106,26 @@ char *Token=NULL;

DestroyString(Token);
}


int CheckForKeyboardInput()
{
char *Tempstr=NULL;
int result=FALSE;

if (STREAMCheckForBytes(StdIn))
{
Tempstr=STREAMReadLine(Tempstr,StdIn);
StripTrailingWhitespace(Tempstr);
if (StrLen(Tempstr))
{
ListAddItem(DownloadQueue,CopyStr(NULL,Tempstr));
if (! (Flags & FLAG_QUIET)) fprintf(stderr,"\r\nQUEUED: %s\n",Tempstr);
result=TRUE;
}
}
DestroyString(Tempstr);

return(result);
}

5 changes: 4 additions & 1 deletion common.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#ifndef MOVGRAB_COMMON
#define MOVGRAB_COMMON
//This is doable through autoconf, but I'm sick of fighting with it
#define Version "3.0.0"
#define Version "3.0.1"

#include "libUseful-2.6/libUseful.h"
#include <string.h>
Expand Down Expand Up @@ -40,10 +40,13 @@ extern char *Username, *Password;
extern char *Proxy;
extern char *ProgName, *CmdLine, *UserAgent;
extern int STREAMTimeout;
extern STREAM *StdIn;

char *FileTypeFromURL(char *URL);
char *BuildURL(char *RetStr, const char *Parent, const char *SubItem);
char *ItemCodeFromFileExtension(char *RetBuf, const char *Default, const char *URL);
void VarsAddDownloadItem(const char *ItemName, const char *URL, ListNode *Vars, int AddFlags);
int CheckForKeyboardInput();


#endif
57 changes: 26 additions & 31 deletions containerfiles.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,53 +119,48 @@ DestroyString(Bandwidth);
int M3UStreamDownload(STREAM *ManifestCon, const char *URL, const char *Title)
{
STREAM *Con=NULL;
char *Tempstr=NULL, *BasePath=NULL, *Line=NULL, *Extn=NULL;
char *Tempstr=NULL, *Line=NULL, *Extn=NULL;
const char *ptr;
ListNode *Segments, *Curr;
int RetVal=FALSE;
double BytesRead=0;

Segments=ListCreate();
ptr=strrchr(URL, '/');
if (ptr)
Line=STREAMReadLine(Line,ManifestCon);
while (Line)
{
BasePath=CopyStrLen(BasePath, URL, ptr - URL);
Line=STREAMReadLine(Line,ManifestCon);
while (Line)
{
StripLeadingWhitespace(Line);
StripTrailingWhitespace(Line);
StripLeadingWhitespace(Line);
StripTrailingWhitespace(Line);

if (*Line != '#')
{
Tempstr=MCopyStr(Tempstr, BasePath, "/", Line, NULL);
ListAddItem(Segments, CopyStr(NULL, Tempstr));
ptr=strrchr(Line,'.');
if (ptr) Extn=CopyStr(Extn, ptr);
}
Line=STREAMReadLine(Line,ManifestCon);
if (*Line != '#')
{
ListAddItem(Segments, BuildURL(NULL, URL, Line));
ptr=strrchr(Line,'.');
if (ptr) Extn=CopyStr(Extn, ptr);
}
Line=STREAMReadLine(Line,ManifestCon);
}

OpenOutputFiles(Title, URL, &BytesRead);
Tempstr=SetStrLen(Tempstr,BUFSIZ);
Curr=ListGetNext(Segments);
while (Curr)
OpenOutputFiles(Title, URL, &BytesRead);
Tempstr=SetStrLen(Tempstr,BUFSIZ);
Curr=ListGetNext(Segments);
while (Curr)
{
Con=ConnectAndRetryUntilDownload(Curr->Item, 0, 0);
if (Con)
{
Con=ConnectAndRetryUntilDownload(Curr->Item, 0, 0);
if (Con)
{
RetVal=TRUE;
TransferItem(Con, Title, URL, "m3u8-stream", 0, 0, &BytesRead, FALSE);
STREAMClose(Con);
}
Curr=ListGetNext(Curr);
RetVal=TRUE;
if (Flags & FLAG_DEBUG) printf("M3U8 Segment: %s\n",Curr->Item);
TransferItem(Con, Title, URL, "m3u8-stream", 0, 0, &BytesRead, FALSE);
STREAMClose(Con);
}
CloseOutputFiles(Extn);
Curr=ListGetNext(Curr);
}
CloseOutputFiles(Extn);

ListDestroy(Segments, DestroyString);

DestroyString(Tempstr);
DestroyString(BasePath);
DestroyString(Line);
DestroyString(Extn);

Expand Down
1 change: 1 addition & 0 deletions display.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "display.h"
#include "outputfiles.h"

pid_t PlayerPid=0;
char *Player=NULL;
Expand Down
4 changes: 3 additions & 1 deletion download.c
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
#include "download.h"
#include "outputfiles.h"
#include "display.h"
#include "containerfiles.h"
#include "servicetypes.h"

/*
Functions relating to connecting to hosts and downloading webpages.
All the HTTP stuff is in here
*/


ListNode *DownloadQueue=NULL;
extern int STREAMTimeout;

STREAM *ConnectAndSendHeaders(const char *URL, int Flags, double BytesRange)
Expand Down
8 changes: 5 additions & 3 deletions download.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@

#include "common.h"

extern char *Proxy;
extern int PlayerLaunchPercent;
extern ListNode *DownloadQueue;

int DownloadItem(const char *URL, const char *Format, const char *Path, int Flags);
int DownloadPage(const char *Path, int Type, const char *Title, int Flags);

int TransferItem(STREAM *Con, const char *Title, const char *URL, const char *Format, double SegmentSize, double DocSize, double *BytesRead, int PrintName);

STREAM *ConnectAndRetryUntilDownload(const char *URL, int Flags, double BytesRead);

extern char *Proxy;
extern int PlayerLaunchPercent;
STREAM *ConnectAndSendHeaders(const char *URL, int Flags, double BytesRange);

#endif
5 changes: 3 additions & 2 deletions extract_text.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

//This function Extracts Text from a line that's found between two specified
//chunks of text 'ItemStart' and 'ItemEnd'
char *GenericExtractFromLine(char *Line, const char *ItemName, const char *ItemStart, const char *ItemEnd, ListNode *Vars, int ExtractFlags)
const char *GenericExtractFromLine(const char *Line, const char *ItemName, const char *ItemStart, const char *ItemEnd, ListNode *Vars, int ExtractFlags)
{
char *ptr, *ptr2, *Token=NULL, *Item=NULL;
const char *ptr;
char *Token=NULL, *Item=NULL, *ptr2;
int len;
int GTF=0;

Expand Down
2 changes: 1 addition & 1 deletion extract_text.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

//This function Extracts Text from a line that's found between two specified
//chunks of text 'ItemStart' and 'ItemEnd'
char *GenericExtractFromLine(char *Line, const char *ItemName, const char *ItemStart, const char *ItemEnd, ListNode *Vars, int Flags);
const char *GenericExtractFromLine(const char *Line, const char *ItemName, const char *ItemStart, const char *ItemEnd, ListNode *Vars, int Flags);
void GenericTitleExtract(const char *Line, ListNode *Vars);

#endif
Expand Down
78 changes: 1 addition & 77 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,88 +26,12 @@
#include "youtube.h"
#include "ehow.h"
#include "display.h"
#include "settings.h"



ListNode *DownloadQueue=NULL;
STREAM *StdIn=NULL;
int Type=TYPE_NONE;

int CheckForKeyboardInput()
{
char *Tempstr=NULL;
int result=FALSE;

if (STREAMCheckForBytes(StdIn))
{
Tempstr=STREAMReadLine(Tempstr,StdIn);
StripTrailingWhitespace(Tempstr);
if (StrLen(Tempstr))
{
ListAddItem(DownloadQueue,CopyStr(NULL,Tempstr));
if (! (Flags & FLAG_QUIET)) fprintf(stderr,"\r\nQUEUED: %s\n",Tempstr);
result=TRUE;
}
}
DestroyString(Tempstr);

return(result);
}





//-------- Go through the processes involved in getting a video file
int GrabMovie(char *Path, int MovType)
{
int i;
char *Proto=NULL, *Server=NULL, *Doc=NULL, *Tempstr=NULL, *Title=NULL;
char *NextPath=NULL;
char *ptr, *Token=NULL;
int Port;
int RetVal=FALSE;

if (!StrLen(Path)) return(FALSE);

Type=MovType;
NextPath=CopyStr(NextPath,Path);
ParseURL(Path,&Proto,&Server,&Tempstr,NULL,NULL,&Doc,NULL);
if (Tempstr) Port=atoi(Tempstr);

if (Proto && (strcasecmp(Proto,"https")==0) )
{
if (! SSLAvailable)
{
printf("SSL NOT COMPILED IN! Switching from 'https' to 'http'\n");
NextPath=MCopyStr(NextPath,"http://",Server,"/",ptr);
}
}

if (Type==TYPE_NONE) Type=IdentifyServiceType(Path);


if (Type==TYPE_NONE)
{
if (! (Flags & FLAG_QUIET)) fprintf(stderr,"Unrecognized url type. Falling Back to 'generic youtube frontend'.\n Try using the -t option to force the service type ( \"movgrab -?\" for more details )\n");
Type=TYPE_GENERIC;
}

NextPath=SiteSpecificPreprocessing(NextPath, Path, Proto, Server, Port, Doc, &Type, &Title, &Flags);
if (DownloadPage(NextPath, Type, Title, Flags)) RetVal=TRUE;

DestroyString(Tempstr);
DestroyString(Server);
DestroyString(Doc);
DestroyString(NextPath);
DestroyString(Token);
DestroyString(Title);
DestroyString(Proto);

return(RetVal);
}





Expand Down
1 change: 1 addition & 0 deletions selectformat.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "selectformat.h"
#include "servicetypes.h"
#include "display.h"

char *GatherMatchingFormats(char *Buffer, char *Type, ListNode *Vars)
{
Expand Down
Loading

0 comments on commit 7991cc8

Please sign in to comment.