-
Notifications
You must be signed in to change notification settings - Fork 5
/
selectformat.c
125 lines (98 loc) · 2.99 KB
/
selectformat.c
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
#include "selectformat.h"
#include "servicetypes.h"
#include "display.h"
#include "settings.h"
char *GatherMatchingFormats(char *Buffer, char *Type, ListNode *Vars)
{
ListNode *Curr;
char *Tempstr=NULL;
Tempstr=CopyStr(Buffer,"");
Curr=ListGetNext(Vars);
while (Curr)
{
if (strncmp(Curr->Tag,"item:",5)==0)
{
if ((! StrValid(Type)) || (strncmp(Curr->Tag,Type,StrLen(Type))==0)) Tempstr=MCatStr(Tempstr,Curr->Tag+5," ",NULL);
}
Curr=ListGetNext(Curr);
}
return(Tempstr);
}
int FmtIDMatches(const char *FmtID, const char *CurrItem, const char *ItemData)
{
//item must be... um... an 'item:'
if (! StrValid(CurrItem)) return(FALSE);
if (strncmp(CurrItem,"item:",5) !=0) return(FALSE);
if (! StrValid(FmtID)) return(TRUE);
if (strcmp(FmtID,"item:*")==0) return(TRUE);
if ((strncmp(CurrItem,FmtID,StrLen(FmtID))==0) && StrValid(ItemData)) return(TRUE);
return(FALSE);
}
//this function compares the video formats found on the page to the list of
//preferences expressed by the user with the '-f' flag, and contained in the
//global variable 'Settings.FormatPreference'
int SelectDownloadFormat(ListNode *Vars, int WebsiteType)
{
ListNode *Curr;
char *ptr, *Tempstr=NULL, *Fmt=NULL, *FmtID=NULL, *Selected=NULL, *p_ItemFormat;
int RetVal=-1, FoundMatch=FALSE, i;
Tempstr=GatherMatchingFormats(Tempstr,"",Vars);
if (! (Flags & FLAG_QUIET))
{
for (i=0; i < 3; i++)
{
if (DisplayAvailableFormats(Vars, Tempstr)) break;
//printf("Connection Refused, sleeping for 20 secs before retry\n");
//sleep(10);
break;
}
}
ptr=GetToken(Settings.FormatPreference,",",&Fmt,0);
while (ptr)
{
if (StrValid(Fmt)) FmtID=MCopyStr(FmtID,"item:",Fmt,NULL);
else FmtID=CopyStr(FmtID,"");
if (Flags & FLAG_DEBUG) fprintf(stderr," %s ",Fmt);
FoundMatch=FALSE;
Curr=ListGetNext(Vars);
while (Curr)
{
if (FmtIDMatches(FmtID,Curr->Tag, (char *) Curr->Item))
{
if (Flags & (FLAG_DEBUG))
{
Tempstr=GatherMatchingFormats(Tempstr,FmtID,Vars);
fprintf(stderr,"... YES! %s\n",Tempstr);
}
if (! StrValid(Selected))
{
Selected=CopyStr(Selected,Curr->Tag);
SetVar(Vars,"ID",(char *) Curr->Item);
}
FoundMatch=TRUE;
break;
}
Curr=ListGetNext(Curr);
}
if ((! FoundMatch) && (Flags & FLAG_DEBUG)) fprintf(stderr,"... no\n",Fmt);
ptr=GetToken(ptr,",",&Fmt,0);
}
if (StrValid(Selected))
{
if (strcmp(Selected,"item:reference")==0) RetVal=TYPE_REFERENCE;
else if (strncmp(Selected,"item:m3u8-stream",16)==0) RetVal=TYPE_CONTAINERFILE_M3U8;
else RetVal=WebsiteType;
}
if (! (Flags & FLAG_TEST_SITES))
{
if (RetVal==-1) fprintf(stderr,"No suitable download format found from '%s'\n\n",Settings.FormatPreference);
else if (RetVal==TYPE_REFERENCE) fprintf(stderr,"Reference to another site: %s\n",GetVar(Vars,"ID"));
else fprintf(stderr,"Selected format %s\n",Selected);
}
//+5 to get past leading 'item:' in variable name
if (StrValid(Selected)) SetVar(Vars,"DownloadFormat",Selected+5);
Destroy(Selected);
Destroy(Tempstr);
Destroy(Fmt);
return(RetVal);
}