|
| 1 | +import instagram |
| 2 | +from flask import request, redirect, render_template, make_response, Response, stream_with_context |
| 3 | +import xmltodict |
| 4 | + |
| 5 | +class Midia: |
| 6 | + def __init__(self, app, stream): |
| 7 | + self.__stream = stream |
| 8 | + self.rotas(app) |
| 9 | + |
| 10 | + def __info(self): |
| 11 | + stream = self.__stream.value |
| 12 | + |
| 13 | + #Recebendo informações dos videos e audios e a url principal |
| 14 | + videoInfo = instagram.Midia.info(stream.id) |
| 15 | + |
| 16 | + #Separando url |
| 17 | + url = videoInfo[:videoInfo.index("\n")] |
| 18 | + videoInfo = videoInfo.replace(url + "\n", "") |
| 19 | + |
| 20 | + #Tratando mpd |
| 21 | + url = url[:url.rindex("/")] |
| 22 | + url = url[:url.rindex("/")+1] |
| 23 | + url = url.replace("https://instagram.fnat1-1.fna.fbcdn.net/hvideo-", "") |
| 24 | + videoInfo = videoInfo.replace("../", url) |
| 25 | + |
| 26 | + videoInfo = xmltodict.parse(videoInfo) |
| 27 | + |
| 28 | + root = videoInfo["MPD"]["Period"]["AdaptationSet"] |
| 29 | + v = root[0]["Representation"] |
| 30 | + a = root[1]["Representation"] |
| 31 | + |
| 32 | + v_type = v["@mimeType"] |
| 33 | + a_type = a["@mimeType"] |
| 34 | + |
| 35 | + v = v["SegmentTemplate"] |
| 36 | + a = a["SegmentTemplate"] |
| 37 | + |
| 38 | + v_url = v["@media"].replace("../", "") |
| 39 | + a_url = a["@media"].replace("../", "") |
| 40 | + |
| 41 | + v = v["SegmentTimeline"]["S"] |
| 42 | + a = a["SegmentTimeline"]["S"] |
| 43 | + |
| 44 | + videos = [{ |
| 45 | + "duration": 0, |
| 46 | + "url": v_url.replace("$Time$", "init"), |
| 47 | + "mime_type": v_type |
| 48 | + }] |
| 49 | + |
| 50 | + audios = [{ |
| 51 | + "duration": 0, |
| 52 | + "url": a_url.replace("$Time$", "init"), |
| 53 | + "mime_type": v_type |
| 54 | + }] |
| 55 | + |
| 56 | + for _v in v: |
| 57 | + videos.append({ |
| 58 | + "duration": _v["@d"], |
| 59 | + "url": v_url.replace("$Time$", _v["@t"]), |
| 60 | + "mime_type": v_type |
| 61 | + }) |
| 62 | + |
| 63 | + for _a in a: |
| 64 | + audios.append({ |
| 65 | + "duration": _a["@d"], |
| 66 | + "url": a_url.replace("$Time$", _a["@t"]), |
| 67 | + "mime_type": v_type |
| 68 | + }) |
| 69 | + |
| 70 | + midia = { |
| 71 | + "videos": videos, |
| 72 | + "audios": audios |
| 73 | + } |
| 74 | + |
| 75 | + return midia |
| 76 | + |
| 77 | + def __midia(self, url, iniciado): |
| 78 | + stream = self.__stream.value |
| 79 | + return instagram.Midia.midia(url, stream.id, iniciado) |
| 80 | + |
| 81 | + def rotas(self, app): |
| 82 | + def getMidiaContent(k, iniciado=False): |
| 83 | + midia = self.__info() |
| 84 | + content = bytes() |
| 85 | + |
| 86 | + for m in midia[k]: |
| 87 | + content += self.__midia(m["url"], iniciado) |
| 88 | + iniciado = True |
| 89 | + return content |
| 90 | + |
| 91 | + def genMidia(k): |
| 92 | + iniciado = False |
| 93 | + with app.app_context(): |
| 94 | + while self.__stream.value is not None: |
| 95 | + content = getMidiaContent(k) |
| 96 | + iniciado = True |
| 97 | + yield content |
| 98 | + |
| 99 | + |
| 100 | + @app.route("/midia/video") |
| 101 | + def getVideo(): |
| 102 | + return Response(stream_with_context(genMidia("videos"))) |
| 103 | + |
| 104 | + @app.route("/midia/audio") |
| 105 | + def getAudio(): |
| 106 | + return make_response( |
| 107 | + getMidiaContent("audios"), |
| 108 | + 200, |
| 109 | + { |
| 110 | + "content-type": "audio/mp4" |
| 111 | + } |
| 112 | + ) |
0 commit comments