This repository has been archived by the owner on Dec 20, 2022. It is now read-only.
generated from othneildrew/Best-README-Template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitter.py
212 lines (205 loc) · 10 KB
/
twitter.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
import requests, re, time, urllib, os, webbrowser, json, tempfile, shutil
from bs4 import BeautifulSoup
from selenium.webdriver.firefox.options import Options
from selenium import webdriver
from selenium.webdriver.common.by import By
from natsort import natsorted
from moviepy.video.io.VideoFileClip import VideoFileClip
from moviepy.video.compositing.concatenate import concatenate_videoclips
def getTwitter(url, response, formato, firefox, decoded):
temp_dir = tempfile.mkdtemp()
if (response == 'Computador'):
if (formato == '.jpg'):
firefox.get(url)
time.sleep(4)
media = firefox.find_elements_by_css_selector('img.css-9pa8cd[alt="Image"]')
name =''
n = 0
for pbs in media:
decoded.append(pbs.get_attribute("src"))
n +=1
size = len(decoded)
continue
count = 0
for i in range(100):
if (os.path.isfile(os.getcwd() + '/' + 'twitter' + '0' + str(i) + '.jpg') == True):
count += 1
continue
else:
break
i = 0
for i in range (size):
if (count == 0):
name = 'twitter' + '0' + str(i) + '.jpg'
urllib.request.urlretrieve(decoded[i], name)
continue
else:
name = 'twitter' + '0' + str(i+count) + '.jpg'
urllib.request.urlretrieve(decoded[i], name)
continue
return True
elif(formato == 'gif'):
firefox.get(url)
time.sleep(4)
decoded.append(firefox.find_element_by_tag_name('video').get_attribute("src"))
size = len(decoded) - 1
name = 'twitter' + '0' + str(size) + ' .mp4'
if (decoded[size] != ''):
count = 0
for i in range(100):
if (os.path.isfile(os.getcwd() + '/' + 'twitter' + '0' + str(i) + '.mp4') == True):
count += 1
name = 'twitter' + '0' + str(count) + '.mp4'
else:
break
urllib.request.urlretrieve(decoded[size], name)
return True
elif(formato == '.mp4'):
firefox.get(url)
time.sleep(10)
log = firefox.execute_script("var performance = window.performance || window.mozPerformance || window.msPerformance || window.webkitPerformance || {}; var network = performance.getEntries() || {}; return network;")
video =''
with open(temp_dir + "/items.json", "w") as jsonFile:
json.dump(log, jsonFile, indent = 6)
jsonFile.close()
with open (temp_dir + "/items.json", "r") as jsonFile:
json_decoded = json.load(jsonFile)
for i in range(len(json_decoded)):
if (re.match(r'https://video.twimg.com/', json_decoded[i]['name']) != None):
video = json_decoded[i]['name']
break
jsonFile.close()
if (re.search(r'https://video.twimg.com/(.*?).m3u8', video) != None):
m3u8c = re.search(r'https://video.twimg.com(.*?)?tag=', video)
encoded = []
if (m3u8c == None):
tipo='m4s'
urllib.request.urlretrieve(video, temp_dir + '/temp.txt')
with open (temp_dir + "/temp.txt", "r") as tempF:
for line in tempF:
if(re.search(r'.ts', line) != None):
encoded.append('https://video.twimg.com' + line)
tipo ='ts'
tempF.close()
directory = os.getcwd()
collection =[]
if(tipo == 'ts'):
for i in range(len(encoded)):
name = temp_dir + '/temp' + '0' + str(i) + '.ts'
urllib.request.urlretrieve(encoded[i], name)
video = VideoFileClip(name)
collection.append(video)
twitter = concatenate_videoclips(collection)
size = len(decoded)
final_name = 'twitter' + '0' + str(size) + '.mp4'
count = 0
for i in range(100):
if (os.path.isfile(directory + '/' + 'twitter' + '0' + str(i) + '.mp4') == True):
count += 1
final_name = 'twitter' + '0' + str(count) + '.mp4'
else:
break
twitter.to_videofile(final_name)
shutil.rmtree(temp_dir)
decoded.append('twittervideo')
return True
else:
shutil.rmtree(temp_dir)
return False
elif (m3u8c != None):
tipo='m4s'
urllib.request.urlretrieve(video, temp_dir + '/temp.txt')
with open (temp_dir + "/temp.txt", "r") as tempF:
for line in tempF:
if(re.search(r'.m3u8', line) != None):
m3u8c2 = 'https://video.twimg.com' + line
continue
tempF.close()
urllib.request.urlretrieve(m3u8c2, temp_dir + '/temp.txt')
with open (temp_dir + "/temp.txt", "r") as tempF:
for line in tempF:
if(re.search(r'.ts', line) != None):
encoded.append('https://video.twimg.com' + line)
tipo = 'ts'
tempF.close()
directory = os.getcwd()
collection =[]
if (tipo == 'ts'):
for i in range(len(encoded)):
name = temp_dir + '/temp' + '0' + str(i) + '.ts'
urllib.request.urlretrieve(encoded[i], name)
video = VideoFileClip(name)
collection.append(video)
twitter = concatenate_videoclips(collection)
size = len(decoded)
final_name = 'twitter' + '0' + str(size) + '.mp4'
count = 0
for i in range(100):
if (os.path.isfile(directory + '/' + 'twitter' + '0' + str(i) + '.mp4') == True):
count += 1
final_name = 'twitter' + '0' + str(count) + '.mp4'
else:
break
twitter.to_videofile(final_name)
shutil.rmtree(temp_dir)
decoded.append('twittervideo')
return True
else:
shutil.rmtree(temp_dir)
return False
else:
tipo='m4s'
encoded = []
with open (temp_dir + "/items.json", "r") as jsonFile:
json_decoded = json.load(jsonFile)
for i in range(len(json_decoded)):
if(re.search('"(.*?).ts', json_decoded[i]['name']) != None):
encoded.append(json_decoded[i]['name'])
tipo='ts'
jsonFile.close()
directory = os.getcwd()
collection =[]
if (tipo == 'ts'):
for i in range(len(encoded)):
name = temp_dir + '/temp' + '0' + str(i) + '.ts'
urllib.request.urlretrieve(encoded[i], name)
video = VideoFileClip(name)
collection.append(video)
twitter = concatenate_videoclips(collection)
size = len(decoded)
final_name = 'twitter' + '0' + str(size) + '.mp4'
count = 0
for i in range(100):
if (os.path.isfile(directory + '/' + 'twitter' + '0' + str(i) + '.mp4') == True):
count += 1
final_name = 'twitter' + '0' + str(count) + '.mp4'
else:
break
twitter.to_videofile(final_name)
shutil.rmtree(temp_dir)
decoded.append('twittervideo')
return True
else:
shutil.rmtree(temp_dir)
return False
elif(response == 'Navegador'):
if (formato == '.jpg'):
firefox.get(url)
time.sleep(5)
decoded = firefox.find_element_by_css_selector('img.css-9pa8cd').get_attribute("src")
if (decoded != ''):
webbrowser.open(decoded)
return True
elif(formato == 'gif'):
firefox.get(url)
time.sleep(5)
decoded = firefox.find_element_by_tag_name('video').get_attribute("src")
if (decoded != ''):
webbrowser.open(decoded)
return True
else:
firefox.quit()
return 0
else:
firefox.quit()
return 0