-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
233 lines (173 loc) · 6.58 KB
/
main.py
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import requests
from bs4 import BeautifulSoup
import html
import time
#web_url = input("Link 1º Ep. post, ")
web_url = "https://www.megacartoons.net/part-i-the-beginning/"
r = requests.get(web_url)
print(r.status_code)
soup = BeautifulSoup(r.content, "html.parser")
class Serie:
def __init__(self):
self.cartoon = '' # <type str>
self.ep_links= () # <type tuple of dicts>
self.episodes= [] # <type set of objects>
# TODO also, search for year&artist
class Episode:
def __init__(self, dictionary):
self.name = dictionary['title'] # name of dictionary.
#self.vurl = dictionary['value']
self.vurl = webLink(dictionary['href'])
# ".mp4" link <str>, next-post url <str>.
ic, bf = extractCoverbrief(dictionary['href'])
self.img_cover = ic # cover image!
self.brief_snp = bf # sinopsis ep.
def clean4Dict(lista):
# creates dictionary:
dict_line = {}
aux = []
for i in range(len(lista)):
aux = lista[i].split('=')
aux[1] = aux[1].replace('"', '')
dict_line[aux[0]] = aux[1]
return dict_line
def createDict(raw_list, num = None):
'''raw_ soup.find("
'''
raw_line = str(raw_list).split(">")
desire_line = raw_line[0].split(" ")
del desire_line[0] # deletes "<dType"
dict_line = clean4Dict(desire_line)
#print(dict_line)
# ".mp4" link <str>, next-post url <str>.
#return dict_line['value'], dict_line['href']
return dict_line #dictionary
def webLink(web_link):
r = requests.get(web_link)
# print(r.status_code)
soup = BeautifulSoup(r.content, "html.parser")
main_in = soup.find("input", attrs={"type": "hidden"})
#nextpost = soup.find("a", attrs={"class": "next"})
# there is not need to select next chapter anymore.
return createDict(main_in)["value"] # , createDict(nextpost)["href"]
def extractCoverbrief(link):
r = requests.get(link)
soup = BeautifulSoup(r.content, "html.parser")
img_line = soup.find("img", attrs={"class": "fp-splash"})
img_path = createDict(img_line)["src"]
sinopsis_aux = soup.find("div", attrs={"class": "item-content toggled"})
sinopsis = str(sinopsis_aux).split('p>')
sinopsis = sinopsis[1].replace("</", "")
#print(sinopsis)
return img_path, sinopsis
# LIMPIEZA DE CAPITULOS con el mismo link del 1er capitulo.
chapters_raw = soup.find("ul", attrs={"class": "video-series-list list-inline"})
#print(chapters_raw)
chapters_rawlist= str(chapters_raw).split('<li><a ')
del chapters_rawlist[0]
aux_crl = []
for i in range(len(chapters_rawlist)):
aux_crl.append(chapters_rawlist[i].split('" '))
aux_crl[-1][2] = aux_crl[-1][2].split('><i ')
aux_crl[-1][2] = aux_crl[-1][2][0]
#print(aux_crl)
aa = []
for i in range(len(aux_crl)):
aaa = []
for j in range(len(aux_crl[i])):
aux_crl[i][j] = aux_crl[i][j].split("=")
if j >= 1:
aux_crl[i][j][1] = aux_crl[i][j][1].replace('"', '')
aaa.append(aux_crl[i][j])
aa.append(aaa)
chapters_list = []
print("Nro de Capitulos:", len(aa))
for z in range(len(aa)):
del aa[z][0]
dict_aux = {}
dict_aux[aa[z][0][0]] = aa[z][0][1] # href = url (link post)
dict_aux[aa[z][1][0]] = aa[z][1][1] # title= name (chapter)
chapters_list.append(dict_aux)
#print(aa)
#print(chapters_list)
ver = Serie()
ver.cartoon = "Samurai Jack"
ver.ep_links= tuple(chapters_list)
#print(ver.ep_links)
vurls, cname, nextp = [], [], []
for i in range(len(ver.ep_links)):
ex_dato = ver.ep_links[i]
ver.episodes.append(ver.Episode(ex_dato))
a = ver.episodes
#print(a[-1])
#print(a[-1].vurl + "\t" + a[-1].name + " " + a[-1].next)
vurls.append(a[-1].vurl)
cname.append(a[-1].name)
#nextp.append(a[-1].next)
#time.sleep(0.5)
#import pandas
# PANDAS: -----------------------------------------
#Domain = ["IT", "DATA_SCIENCE", "NEYWORKING"]
#domain_dict = {'Mp4 URLs': vurls, 'Episode': cname, 'Next C.': nextp}
# delet this ^ .
#print(domain_dict)
#data_frame = pandas.DataFrame(domain_dict)
#data_frame.to_csv(ver.cartoon + '.csv')
#--------------------------------------------------
from xml.etree.ElementTree import Element as ele, SubElement as subele
import xml.etree.ElementTree as xee
inputLink = '.mp4' # It's parameter of funct.
#top = 'playlist'
playlist = ele('playlist') # (top)
playlist.set('xmlns', "http://xspf.org/ns/0/")
playlist.set('xmlns:vlc', "http://www.videolan.org/vlc/playlist/ns/0/")
playlist.set('version', "1")
list_rep = subele(playlist, 'title')
list_rep.text = "Lista de reproducción"
play_list = subele(playlist, 'trackList')
# repetir desde aqui ----- \/ , iterations.
# def vlcPlaylist(convert):
# ↓ (convert.episodes)
for i in range(len(ver.episodes)):
inputLink = ver.episodes[i].vurl
pista = subele(play_list, 'track') # TRACK
lugar = subele(pista, 'location')
lugar.text = inputLink #TODO def parameter.
title = subele(pista, 'title')
title.text = ver.episodes[i].name
album = subele(pista, 'album')
album.text = ver.cartoon
track_num = subele(pista, 'trackNum')
track_num.text = str(i + 1)
comment = subele(pista, 'annotation')
comment.text = ver.episodes[i].brief_snp
cover = subele(pista, "image")
cover.text = ver.episodes[i].img_cover
ext_app = "http://www.videolan.org/vlc/playlist/0"
extensionapp = subele(pista, 'extension')
extensionapp.set('application', ext_app)
vlc_id = subele(extensionapp, 'vlc:id')
vlc_id.text = str(i) # IMPORTANT, Nro track!!
# TODO ^ position of the list!!
vlc_opt = subele(extensionapp, 'vlc:option')
vlc_opt.text = 'network-caching=1000'
# --- extension (o)ut o(f) trackLis(t): ~~~|
extensionapp_oft = subele(playlist, 'extension')
ext_app = "http://www.videolan.org/vlc/playlist/0"
extensionapp_oft.set('application', ext_app)
# items:
# <vlc:item tid="nro_idex_list_inputLink"/>
# for...
for trackid in range(len(ver.episodes)):
inside_i = 'vlc:item tid="' + str(trackid) + '"'
vlc_item = subele(extensionapp_oft, inside_i)
print(playlist) # print prittify(playlist)
# create a new XML file with the results: ↓
#tree = ET.ElementTree(vehicles)
#tree.write("vehicle_file.xml", xml_declaration=True, encoding='utf-8', method="xml")
# ^ code from https://norwied.wordpress.com/2013/08/27/307/
mydata = xee.ElementTree(playlist)
mydata.write(ver.cartoon + ".xspf", # nameSerie
xml_declaration=True,
encoding='utf-8', method="xml")
#print(mydata) # it prints xeEtEt object...