├── .gitignore ├── LICENSE.txt ├── example.gif ├── links.json ├── readme.md ├── requirements.txt ├── static ├── NotoColorEmoji.ttf ├── copy.svg ├── css │ └── main.css ├── favicon.ico ├── main.js └── style.css ├── templates ├── base.html ├── default.html ├── image.html ├── index.html ├── inline.html ├── latest.html ├── stats.html ├── text.html └── video.html ├── twitfix.ini ├── twitfix.py ├── twitfix.service └── wsgi.py /.gitignore: -------------------------------------------------------------------------------- 1 | config.json 2 | __pycache__ 3 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE 2 | Version 2, December 2004 3 | 4 | Copyright (C) 2004 Sam Hocevar 5 | 6 | Everyone is permitted to copy and distribute verbatim or modified 7 | copies of this license document, and changing it is allowed as long 8 | as the name is changed. 9 | 10 | DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE 11 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 12 | 13 | 0. You just DO WHAT THE FUCK YOU WANT TO. 14 | 15 | -------------------------------------------------------------------------------- /example.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/daisyUniverse/TwitFix/5b58c0d9bc77279fd0526568650022f948d5dd2b/example.gif -------------------------------------------------------------------------------- /links.json: -------------------------------------------------------------------------------- 1 | { 2 | "test": "test" 3 | } -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # TwitFix 2 | 3 | Basic flask server that serves fixed twitter video embeds to desktop discord by using either the Twitter API or Youtube-DL to grab tweet video information. This also automatically embeds the first link in the text of non video tweets (API Only) 4 | 5 | ## How to use (discord side) 6 | 7 | just put the url to the server, and directly after, the full URL to the tweet you want to embed 8 | 9 | **I now have a copy of this running on a Linode server, you can use it via the following url** 10 | 11 | ``` 12 | https://fxtwitter.com/[twitter video url] or [last half of twitter url] (everything past twitter.com/) 13 | ``` 14 | 15 | You can also simply type out 'fx' directly before 'twitter.com' in any valid twitter video url, and that will convert it into a working TwitFix url, for example: 16 | 17 | ![example](example.gif) 18 | 19 | **Note**: If you enjoy this service, please considering donating via [Ko-Fi](https://ko-fi.com/robin_universe) to help cover server costs 20 | 21 | ## Child Projects: 22 | 23 | [TwitFix-Bot](https://github.com/robinuniverse/TwitFix-Bot) - A discord bot for automatically converting normal twitter links posted by users into twitfix links 24 | 25 | [TwitFix-Extension](https://github.com/robinuniverse/TwitFix-Extension) - A browser extention that lets you right click twitter videos to copy a twitfix link to your clipboard 26 | 27 | # Monthly Contributors 28 | 29 | TwitFix is run for free, period, I have no plans to monetize it directly in any way ( no ads, no premium accounts with more features ) so I rely on donations to keep TwitFix running, and I have created the option to [donate on a monthly basis using my KoFi](https://ko-fi.com/robin_universe#tier16328580186740) 30 | 31 | 32 | 33 | Here's a a list of the people who help to keep this project alive! ( current total monthly - $49!!! ) 34 | 35 | 1. [$3] First Contributor and Twitter Funnyman **Chris Burwell** ( [@countchrisdo](https://twitter.com/countchrisdo) on Twitter ) 36 | 37 | 2. [$9] Previously highest Contributor, Suspciously wealthy furry, and a very loving friend **Vectrobe** ( [@Vectrobe](https://twitter.com/Vectrobe) on Twitter ) 38 | 39 | 3. [$10] New highest monthly contributor, **helloitscrash**! 40 | 41 | 4. [$6] A Mysterious and **Anonymous** contributor... 42 | 43 | 5. [$10] One of the highest contributors, **Ryan Vilbrandt**! 44 | 45 | 6. [$3] **Starcat13**, the one with the coolest sounding name 46 | 47 | 7. [$5] THE LIGHT THROUGH WHICH GOD SPEAKS TO THIS EARTH: **Statek** 48 | 49 | 8. [$3] **Impulse**, probably the source cheat 50 | 51 | 9. [$3] a STRONG contendor for coolest name, "**Lost in Art & Magic**" 52 | 53 | ## How to run (server side) 54 | 55 | this script uses the youtube-dl python module, along with flask, twitter and pymongo, so install those with pip (you can use `pip install -r requirements.txt`) and start the server with `python twitfix.py` 56 | 57 | I have included some files to give you a head start on setting this server up with uWSGI, though if you decide to use uWSGI I suggest you set up mongoDB link caching 58 | 59 | ### Config 60 | 61 | TwitFix generates a config.json in its root directory the first time you run it, the options are: 62 | 63 | **API** - This will be where you put the credentials for your twitter API if you use this method 64 | 65 | **database** - This is where you put the URL to your mongoDB database if you are using one 66 | 67 | **link_cache** - (Options: **db**, **json**) 68 | 69 | - **db**: Caches all links to a mongoDB database. This should be used it you are using uWSGI and are not just running the script on its own as one worker 70 | - **json**: This saves cached links to a local **links.json** file 71 | 72 | **method** - ( Options: **youtube-dl**, **api**, **hybrid** ) 73 | 74 | - **youtube-dl**: the original method for grabbing twitter video links, this uses a guest token provided via youtube-dl and should work well for individual instances, but may not scale up to a very large amount of usage 75 | 76 | - **api**: this directly uses the twitter API to grab tweet info, limited to 900 calls per 15m 77 | - **hybrid**: This will start off by using the twitter API to grab tweet info, but if the rate limit is reached or the api fails for any other reason it will switch over to youtube-dl to avoid downtime 78 | 79 | **color** - Accepts a hex formatted color code, can change the embed color 80 | 81 | **appname** - Can change the app name easily wherever it's shown 82 | 83 | **repo** - used to change the repo url that some links redirect to 84 | 85 | **url** - used to tell the user where to look for the oembed endpoint, make sure to set this to your public facing url 86 | 87 | This project is licensed under the **Do What The Fuck You Want Public License** 88 | 89 | 90 | 91 | ## Other stuff 92 | 93 | Going to `https://fxtwitter.com/latest/` will present a page that shows the all the latest tweets that were added to the database, use with caution as results may be nsfw! Current page created by @DorukSaga 94 | 95 | Using the `/dir/` endpoint will return a redirect to the direct MP4 link, this can be useful for downloading a video 96 | 97 | Using the `/dl/` or appending a `.mp4` will make the server download the video and return a static, locally hosted copy 98 | 99 | Using the subdomain `d.fxtwitter.com/` will redirect to a direct MP4 url hosted on Twitter 100 | 101 | Using the `/info/` endpoint will return a json that contains all video info that youtube-dl can grab about any given video 102 | 103 | Using `/other/` will attempt to run the twitter embed stuff on other websites videos - This is mostly experimental and doesn't really work for now 104 | 105 | Using `/api/latest/` will return a json with the latest tweet added to the database. Takes params `?tweets=INT&=pageINT` to return multiple 106 | 107 | Using `/api/top/` will return a json with the most hit tweet in the database. Takes params `?tweets=INT&=pageINT` to return multiple 108 | 109 | Using `/api/stats/` will return a json with some stats about TwitFix's activity (embeds, new cached links, API hits, downloads). Takes param `?=date"YYYY-MM-DD"` to return a specific day, otherwise will return today's stats to far 110 | 111 | Advanced embeds are provided via a `/oembed.json?` endpoint - This is manually pointing at my server in `/templates/index.html` and should be changed from `https://fxtwitter.com/` to whatever your domain is 112 | 113 | We check for t.co links in non video tweets, and if one is found, we direct the discord useragent to embed that link directly, this means that twitter links containing youtube / vimeo links will automatically embed those as if you had just directly linked to that content 114 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | Flask 2 | youtube_dl 3 | pymongo 4 | twitter 5 | flask_cors -------------------------------------------------------------------------------- /static/NotoColorEmoji.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/daisyUniverse/TwitFix/5b58c0d9bc77279fd0526568650022f948d5dd2b/static/NotoColorEmoji.ttf -------------------------------------------------------------------------------- /static/copy.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /static/css/main.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin-right: 40%; 3 | background: color 0; 4 | } -------------------------------------------------------------------------------- /static/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/daisyUniverse/TwitFix/5b58c0d9bc77279fd0526568650022f948d5dd2b/static/favicon.ico -------------------------------------------------------------------------------- /static/main.js: -------------------------------------------------------------------------------- 1 | var tweetCount = 1, 2 | page = 0, 3 | loading = false, 4 | bigArray = [], 5 | isNSFWSHOW = false; 6 | const iosHeight = () => { 7 | document.documentElement.style.setProperty("--ios-height", window.innerHeight + "px"); 8 | }; 9 | window.addEventListener("resize", iosHeight); 10 | 11 | window.onload = () => { 12 | //ios height 13 | iosHeight(); 14 | const contwarn = document.querySelector("#contwarn"); 15 | const notsafe = document.querySelector("#notsafe"); 16 | if (document.cookie.includes("NSFW=true")) { 17 | notsafe.checked = true; 18 | isNSFWSHOW = true; 19 | } 20 | if (document.cookie.includes("always=true")) { 21 | contwarn.checked = true; 22 | forNow(); 23 | } 24 | contwarn.addEventListener("change", () => { 25 | if (contwarn.checked) 26 | addCookie("always", true); 27 | else 28 | addCookie("always", false); 29 | }) 30 | notsafe.addEventListener("change", () => { 31 | if (notsafe.checked) { 32 | document.querySelectorAll(".nsfw").forEach(e => e.classList.add("noff")); 33 | addCookie("NSFW", true); 34 | isNSFWSHOW = true; 35 | } 36 | else { 37 | document.querySelectorAll(".noff").forEach(e => e.classList.remove("noff")); 38 | isNSFWSHOW = false; 39 | addCookie("NSFW", false); 40 | } 41 | 42 | }) 43 | } 44 | 45 | function addCookie(name, state) { 46 | if (state) 47 | document.cookie = `${name}=${state}; max-age=15780000; SameSite=None; Secure`; 48 | else 49 | document.cookie = `${name}=`; 50 | } 51 | 52 | function cookieTime() { 53 | document.querySelector("#contwarn").checked = true; 54 | addCookie("always", true); 55 | forNow(); 56 | } 57 | function forNow() { 58 | document.querySelector("#block").style.display = "none"; 59 | fetchNApply(page) 60 | page++; 61 | const tweetCont = document.querySelector(".tweetCont"); 62 | tweetCont.onscroll = async () => { 63 | if (tweetCont.scrollTop > tweetCont.scrollHeight - tweetCont.offsetHeight - 100 && loading == false) { 64 | await fetchNApply(page); 65 | //console.log(page); 66 | page++; 67 | } 68 | }; 69 | } 70 | function fetchNApply(page) { 71 | try { 72 | loading = true; 73 | fetch(`https://fxtwitter.com/api/latest/?tweets=10&page=${page}`) 74 | .then(response => response.json()) 75 | .then(data => { 76 | data.forEach(e => createTweet(e)); 77 | }) 78 | .then(setTimeout(() => loading = false, 500)); 79 | } catch (error) { 80 | console.log(error); 81 | alert(error) 82 | } 83 | 84 | } 85 | 86 | function imgPrev(img) { 87 | const prevImgCont = document.querySelector('.previmgcont'); 88 | img.addEventListener('click', () => { 89 | const clone = img.cloneNode(true); 90 | clone.removeAttribute("width"); 91 | clone.removeAttribute("height"); 92 | clone.className = "previmg"; 93 | prevImgCont.innerHTML = ""; 94 | prevImgCont.appendChild(clone); 95 | prevImgCont.style.display = ''; 96 | }); 97 | } 98 | /* 99 | cont ={ 100 | inner: text 101 | src: link 102 | lazy: bool 103 | href: link 104 | video: link 105 | } 106 | */ 107 | function createEl(tag, cls, cont) { 108 | const el = document.createElement(tag); 109 | if (cls) 110 | el.className = cls; 111 | if (cont) { 112 | if (cont.inner) 113 | el.innerHTML = cont.inner; 114 | if (cont.src) 115 | el.src = cont.src; 116 | if (cont.lazy) 117 | el.setAttribute("loading", "lazy"); 118 | if (cont.href) 119 | el.setAttribute("href", cont.href), 120 | el.setAttribute("rel", "noreferrer"), 121 | el.setAttribute("target", "_blank"); 122 | if (cont.video) { 123 | el.setAttribute("preload", "auto"); 124 | el.setAttribute("controls", ""); 125 | el.setAttribute("disablePictureInPicture", ""); 126 | el.setAttribute("webkit-playsinline", ""); 127 | el.setAttribute("playsinline", ""); 128 | const source = document.createElement("source"); 129 | source.src = cont.video; 130 | source.setAttribute("type", "video/mp4"); 131 | el.appendChild(source); 132 | } 133 | } 134 | return el; 135 | } 136 | 137 | function createTweet(json) { 138 | if (!bigArray.includes(json["tweet"])) { 139 | const tweet = createEl("div", "tweet"); 140 | tweet.id = "t" + tweetCount; 141 | 142 | const auth = createEl("div", "auth"); 143 | 144 | const aimage = createEl("img", "aimage", { 145 | src: json["pfp"], 146 | lazy: true 147 | }); 148 | 149 | const aname = createEl("a", "aname", { 150 | href: `https://twitter.com/${json['screen_name']}` 151 | }); 152 | 153 | aname.appendChild(createEl("div", undefined, { 154 | inner: json['uploader'] 155 | })); 156 | 157 | aname.appendChild(createEl("div", undefined, { 158 | inner: "@" + json['screen_name'] 159 | })); 160 | 161 | const type = createEl("a", "type", { inner: json["type"], href: json["tweet"] }); 162 | 163 | auth.appendChild(aimage); 164 | auth.appendChild(aname); 165 | auth.appendChild(type); 166 | 167 | tweet.appendChild(auth); 168 | 169 | json["description"] = json["description"].replaceAll(/http.*t.co\S+/g, ""); 170 | 171 | 172 | if (json["description"] != "") { 173 | const desc = createEl("div", "desc", { inner: json["description"] }); 174 | tweet.appendChild(desc); 175 | } 176 | 177 | if (json["nsfw"]) { //beware 178 | var nsfw = createEl("div", "nsfw"); 179 | const ncont = createEl("div", "ncont"); 180 | const ninfo = createEl("div", "ninfo", { inner: "This is a NSFW Tweet
Press \"Show me\" if you want to see it" }); 181 | var nshow = createEl("div", "nshow", { inner: "Show me" }); 182 | ncont.appendChild(ninfo); 183 | ncont.appendChild(nshow); 184 | nsfw.appendChild(ncont); 185 | if (isNSFWSHOW === true) 186 | nsfw.classList.add("noff"); 187 | } 188 | 189 | 190 | switch (json["type"]) { 191 | case "Text": 192 | //so empty 193 | break; 194 | case "Image": 195 | if (json["images"][4] > "1" && json["images"][4]) { //multiple images!!?? 196 | const grid = createEl("div", "imgCont"); 197 | for (let i = 0; i < json["images"][4]; i++) 198 | grid.appendChild(createEl("img", "media", { 199 | src: json["images"][i], 200 | lazy: true 201 | })); 202 | //console.log(json["images"][4]) 203 | if (nsfw) 204 | nsfw.appendChild(grid); 205 | else 206 | tweet.appendChild(grid); 207 | } 208 | else { 209 | const media = createEl("img", "media", { 210 | src: json["thumbnail"], 211 | lazy: true 212 | }); 213 | if (nsfw) 214 | nsfw.appendChild(media); 215 | else 216 | tweet.appendChild(media); 217 | } 218 | 219 | break; 220 | case "Video": 221 | const video = createEl("video", "media", { 222 | video: json["url"] 223 | }); 224 | if (nsfw) 225 | nsfw.appendChild(video); 226 | else 227 | tweet.appendChild(video); 228 | break; 229 | default: 230 | const video2 = createEl("video", "media", { 231 | video: json["url"] 232 | }); 233 | if (nsfw) 234 | nsfw.appendChild(video2); 235 | else 236 | tweet.appendChild(video2); 237 | //console.log("this should not happen!"); 238 | break; 239 | } 240 | 241 | if (nsfw) { 242 | tweet.appendChild(nsfw); 243 | nshow.addEventListener("click", () => { 244 | nsfw.classList.add("noff"); 245 | }); 246 | } 247 | 248 | 249 | const qrtob = json["qrt"]; 250 | 251 | if ((Object.keys(qrtob).length === 0 && Object.getPrototypeOf(qrtob) === Object.prototype) == false) { 252 | //console.log(json["qrt"]); 253 | const qrt = createEl("div", "quote"); 254 | const qname = createEl("div", "qname", { inner: `QRT of : ${qrtob.handle} (@${qrtob.screenname})` }); 255 | json["qrt"]["desc"] = json["qrt"]["desc"].replaceAll(/http.*t.co\S+/g, ""); 256 | const qdesc = createEl("div", "qdesc", { inner: qrtob.desc }); 257 | qrt.appendChild(qname); 258 | qrt.appendChild(qdesc); 259 | tweet.appendChild(qrt); 260 | } 261 | 262 | 263 | const meta = createEl("div", "meta"); 264 | 265 | const rts = createEl("div", "cont", { inner: `${json["rts"]} Retweets` }); 266 | const lks = createEl("div", "cont", { inner: `${json["likes"]} Likes` }); 267 | 268 | const share = createEl("img", "share", { src: "https://fxtwitter.com/copy.svg" }); 269 | 270 | meta.appendChild(rts); 271 | meta.appendChild(lks); 272 | meta.appendChild(share); 273 | tweet.appendChild(meta); 274 | bigArray.push(json["tweet"]); 275 | document.querySelector(".tweetCont").appendChild(tweet); 276 | document.querySelectorAll('img:not(.aimage,.share)').forEach(img => { 277 | imgPrev(img); 278 | }); 279 | share.addEventListener("click", () => 280 | navigator.clipboard.writeText(json["tweet"].replace("https://t", "https://fxt")) 281 | ); 282 | 283 | tweetCount++; 284 | } else { 285 | if (bigArray.length > 100) //pro memory management 😎 286 | bigArray = []; 287 | } 288 | } -------------------------------------------------------------------------------- /static/style.css: -------------------------------------------------------------------------------- 1 | @font-face { 2 | font-family: NotoColorEmojiLimited; 3 | unicode-range: U+1F1E6-1F1FF; 4 | src: url("https://fxtwitter.com/font.ttf"); 5 | } 6 | 7 | :root { 8 | --ios-height: 100vh; 9 | --top-height: calc(4.5vh + 1.25vw); 10 | } 11 | 12 | body { 13 | margin: 0; 14 | width: 100vw; 15 | height: 100vh; 16 | height: var(--ios-height); 17 | overflow: hidden; 18 | } 19 | 20 | 21 | a { 22 | color: unset; 23 | text-decoration: unset; 24 | } 25 | 26 | a, 27 | a:link, 28 | a:focus, 29 | a:visited, 30 | a:active, 31 | a:-webkit-any-link, 32 | :link { 33 | color: unset; 34 | text-decoration: unset; 35 | } 36 | 37 | a:hover { 38 | color: #8ebf42; 39 | } 40 | 41 | ::-webkit-scrollbar { 42 | width: 1vmin; 43 | 44 | } 45 | 46 | ::-webkit-scrollbar-track { 47 | background: #171717; 48 | border-radius: .5vmin; 49 | margin: 2vmin 0 2vmin; 50 | } 51 | 52 | ::-webkit-scrollbar-thumb { 53 | background: #e8e8e8; 54 | border-radius: .5vmin; 55 | } 56 | 57 | ::-webkit-scrollbar-thumb:hover { 58 | background: #8ebf42; 59 | } 60 | 61 | ::selection { 62 | color: #171717; 63 | background-color: #e8e8e8; 64 | } 65 | 66 | .base { 67 | display: flex; 68 | flex-direction: column; 69 | width: 100%; 70 | height: 100%; 71 | font-family: 'Paytone One', 'NotoColorEmojiLimited', sans-serif; 72 | background-color: #222222; 73 | color: #e8e8e8; 74 | } 75 | 76 | .top { 77 | height: var(--top-height); 78 | display: flex; 79 | flex-direction: column; 80 | justify-content: center; 81 | text-align: center; 82 | box-sizing: border-box; 83 | font-size: 7vmin; 84 | padding: 1vmin 0 1vmin; 85 | /*margin: 0 20vw 0; 86 | border-bottom: #e8e8e8 .1vmin solid;*/ 87 | box-shadow: rgba(0, 0, 0, 0.2) 0px 1px 2px 0px; 88 | } 89 | 90 | .top:hover { 91 | color: #8ebf42; 92 | cursor: pointer; 93 | } 94 | 95 | .bottom { 96 | flex: 1; 97 | display: flex; 98 | flex-direction: row; 99 | } 100 | 101 | .tweetCont { 102 | flex: 2; 103 | padding: calc(1vh + .25vw) 10% 0; 104 | 105 | overflow-y: auto; 106 | max-height: calc(var(--ios-height) - var(--top-height) - calc(1vh + .25vw)); 107 | } 108 | 109 | .tweet { 110 | display: flex; 111 | position: relative; 112 | flex-direction: column; 113 | background-color: #171717; 114 | margin: 0 0 calc(2vh + .5vw); 115 | padding: 1vmin 0 1vmin; 116 | box-shadow: rgba(0, 0, 0, 0.4) 0px 4px 7px; 117 | font-size: 18px; 118 | } 119 | 120 | .side { 121 | flex: 1; 122 | overflow-y: auto; 123 | max-height: calc(var(--ios-height) - var(--top-height) - calc(1vh + .25vw)); 124 | } 125 | 126 | .by { 127 | color: #8ebf42; 128 | } 129 | 130 | .by:hover { 131 | color: #F74843; 132 | } 133 | 134 | /* Tweet */ 135 | .desc { 136 | padding: 0 calc(1.5vh + .25vw) 0; 137 | } 138 | 139 | .auth, 140 | .meta { 141 | display: flex; 142 | flex-direction: row; 143 | gap: 1%; 144 | font-size: 1.2em; 145 | padding: 0 calc(.5vh + .125vw) 0; 146 | } 147 | 148 | .aname { 149 | display: flex; 150 | flex-direction: column; 151 | justify-content: space-evenly; 152 | } 153 | 154 | .aname, 155 | .type { 156 | flex: 1; 157 | } 158 | 159 | .type { 160 | text-align: right; 161 | margin: auto 2% auto; 162 | } 163 | 164 | .aimage { 165 | border-radius: 50%; 166 | border: #8ebf42 .3vmin solid; 167 | height: 100%; 168 | width: auto; 169 | margin: auto; 170 | /* No Drag */ 171 | -webkit-user-drag: none; 172 | -khtml-user-drag: none; 173 | -moz-user-drag: none; 174 | -o-user-drag: none; 175 | } 176 | 177 | .nsfw { 178 | display: flex; 179 | align-items: center; 180 | justify-content: center; 181 | overflow: hidden; 182 | } 183 | 184 | .ncont { 185 | position: absolute; 186 | z-index: 1; 187 | bottom: 20%; 188 | display: flex; 189 | flex-direction: column; 190 | justify-content: center; 191 | align-items: center; 192 | gap: 2vmin; 193 | background-color: #17171788; 194 | border-radius: .5vmin; 195 | padding: 1vw; 196 | 197 | } 198 | 199 | .ninfo { 200 | text-align: center; 201 | } 202 | 203 | .nshow { 204 | padding: 1vh 1vw; 205 | background-color: #171717; 206 | border: solid .5vmin #8ebf42; 207 | color: #e8e8e8; 208 | border-radius: .5vmin; 209 | transition-duration: .2s; 210 | transition-timing-function: ease-in-out; 211 | cursor: pointer; 212 | user-select: none; 213 | font-size: 1em; 214 | } 215 | 216 | 217 | .nshow:hover { 218 | transform: scale(1.1); 219 | border: solid .5vmin #bf4242; 220 | } 221 | 222 | .nshow:active { 223 | transform: scale(.98); 224 | } 225 | 226 | .nsfw .media { 227 | filter: blur(30px); 228 | 229 | } 230 | 231 | .noff div:not(.imgCont) { 232 | display: none; 233 | } 234 | 235 | .noff .media { 236 | display: unset; 237 | filter: unset; 238 | } 239 | 240 | .imgCont { 241 | display: flex; 242 | flex-direction: row; 243 | justify-content: center; 244 | flex-wrap: wrap; 245 | 246 | 247 | } 248 | 249 | .imgCont .media { 250 | max-width: 50%; 251 | max-height: 35vh; 252 | } 253 | 254 | .media { 255 | box-sizing: border-box; 256 | width: 100%; 257 | height: auto; 258 | max-height: 60vh; 259 | object-fit: scale-down; 260 | background-color: black; 261 | 262 | } 263 | 264 | .quote { 265 | margin: auto; 266 | width: 90%; 267 | border-radius: .5vmin; 268 | border: #222222 solid .5vmin; 269 | padding: .5vmin; 270 | } 271 | 272 | .qname { 273 | color: #8ebf42; 274 | } 275 | 276 | .cont, 277 | .date { 278 | margin: auto 0 auto; 279 | 280 | } 281 | 282 | .date { 283 | margin-left: 2%; 284 | } 285 | 286 | .share { 287 | margin-left: auto; 288 | height: 2em; 289 | transition-duration: .2s; 290 | transition-timing-function: ease-in-out; 291 | } 292 | 293 | .share:hover { 294 | transform: scale(.8); 295 | } 296 | 297 | .share:active { 298 | transform: scale(2); 299 | } 300 | 301 | .auth, 302 | .desc, 303 | .quote { 304 | margin-bottom: 2vmin; 305 | } 306 | 307 | /* Settings */ 308 | .settings, 309 | .info { 310 | font-size: 35px; 311 | padding: 1vmin; 312 | } 313 | 314 | .settings { 315 | user-select: none; 316 | display: flex; 317 | flex-direction: column; 318 | } 319 | 320 | .opt { 321 | font-size: .49em; 322 | user-select: none; 323 | white-space: nowrap; 324 | flex-wrap: nowrap; 325 | } 326 | 327 | .Iinner { 328 | font-size: .5em; 329 | } 330 | 331 | /* Preview */ 332 | 333 | img:not(.previmg, .aimage) { 334 | cursor: pointer; 335 | transition-duration: .2s; 336 | transition-timing-function: ease-in-out; 337 | } 338 | 339 | .previmgcont { 340 | position: fixed; 341 | display: flex; 342 | justify-content: center; 343 | flex-direction: column; 344 | top: 0%; 345 | left: 0%; 346 | width: 100%; 347 | height: 100%; 348 | z-index: 101; 349 | background-color: #171717b7; 350 | } 351 | 352 | .previmg { 353 | margin: auto; 354 | cursor: zoom-out !important; 355 | max-width: 100vw; 356 | max-height: 100vh; 357 | min-width: 100vw; 358 | object-fit: contain; 359 | } 360 | 361 | /* Block */ 362 | 363 | #block { 364 | position: fixed; 365 | box-sizing: border-box; 366 | width: 100%; 367 | height: 100%; 368 | z-index: 99; 369 | background: #222222; 370 | display: flex; 371 | flex-direction: column; 372 | align-items: center; 373 | justify-content: center; 374 | font-size: calc(1.75vh + .25vw); 375 | text-align: center; 376 | } 377 | 378 | #block .warn { 379 | font-size: 2em; 380 | color: #F74843; 381 | text-align: center; 382 | } 383 | 384 | .boptions { 385 | display: flex; 386 | flex-direction: row; 387 | gap: 1.5vmin; 388 | } 389 | 390 | .boptions>div { 391 | padding: 2vh 2vw; 392 | border: solid .5vmin #c5c5c5; 393 | color: #e8e8e8; 394 | border-radius: .5vmin; 395 | transition-duration: .15s; 396 | transition-timing-function: ease-in-out; 397 | cursor: pointer; 398 | user-select: none; 399 | font-size: 1em; 400 | } 401 | 402 | .boptions>div:hover { 403 | transform: scale(1.02); 404 | border: solid .5vmin #8ebf42; 405 | } 406 | 407 | .boptions>div:active { 408 | transform: scale(.98); 409 | } 410 | 411 | @media (max-aspect-ratio: 4/4), 412 | (max-height: 500px), 413 | (max-width: 1100px) { 414 | .side { 415 | display: none; 416 | } 417 | 418 | .tweet { 419 | font-size: 12px; 420 | } 421 | 422 | .tweetCont { 423 | padding: 0 5% 0; 424 | } 425 | 426 | .boptions { 427 | flex-direction: column; 428 | } 429 | } -------------------------------------------------------------------------------- /templates/base.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | {% block head %}{% endblock %} 5 | 6 | 7 | {% block body %}{% endblock %} 8 | 9 | -------------------------------------------------------------------------------- /templates/default.html: -------------------------------------------------------------------------------- 1 | {% extends 'base.html' %} 2 | 3 | {% block head %} 4 | {{ appname }} 5 | 6 | 7 | 8 | 9 | 10 | {% endblock %} 11 | 12 | {% block body %} 13 | You will be redirected to this projects github page in a moment. Or click here. 14 | {% endblock %} 15 | -------------------------------------------------------------------------------- /templates/image.html: -------------------------------------------------------------------------------- 1 | {% extends 'base.html' %} 2 | 16 | {% block head %} 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | {% endblock %} 32 | 33 | {% block body %} 34 | Redirecting you to the tweet in a moment. Or click here. 35 | {% endblock %} 36 | -------------------------------------------------------------------------------- /templates/index.html: -------------------------------------------------------------------------------- 1 | {% extends 'base.html' %} 2 | 3 | {% block head %} 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | {% endblock %} 34 | 35 | {% block body %} 36 | Redirecting you to the tweet in a moment. Or click here. 37 | {% endblock %} 38 | -------------------------------------------------------------------------------- /templates/inline.html: -------------------------------------------------------------------------------- 1 | {% extends 'base.html' %} 2 | 3 | {% block head %} 4 | TwitFix {{ page }} 5 | 12 | 106 | 107 | 108 | {% endblock %} 109 | 110 | {% block body %} 111 | 112 |
113 |
114 | Potentially NSFW Content: Click here if you are over 18 115 |
116 |
117 |
TwitFix {{ page }} Video
118 |
WARNING: Video content is not screened so it may contain NSFW Content
119 | 122 |
123 |
Another?
124 |
Copy Link!
125 |
126 |
127 | Original Tweet 128 |
129 | Original Author: {{ user }} 130 |
131 |
132 | 133 | {% endblock %} -------------------------------------------------------------------------------- /templates/latest.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | Twitfix Latest 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 20 | 21 |
22 |
23 |

Warning!!

24 |

Twitfix only filters the content specified as NSFW.

25 |

Following content might be NSFW.

26 |

Prepare yourself mentally and pick one of the options

27 |

Do you want to see the latest twitfix requests?

28 |
29 |
30 |
Yes, don't ask again!
31 |
Yes, for now.
32 |
No, please take me 33 | out of here
34 |
35 |
36 | TwitFix 37 | Latest 38 |
39 |
40 |
TwitFix
41 |
42 | Basic flask server that serves fixed twitter video embeds to desktop discord by using either the 43 | Twitter API or Youtube-DL to grab tweet video information. This also automatically embeds the first 44 | link in the text of non video tweets (API Only). 45 |
46 |
47 |
TwitFix Latest
48 |
49 | Shows the most recent TwitFix API requests in a list like manner. 50 |
51 |
52 |
53 | TwitFix by Robin 54 | ✨Universe

55 | Site Design by Doruk 57 |
58 |
59 |
60 |
61 |
62 |
Settings
63 |
64 | 65 | 66 |
67 |
68 | 69 | 70 |
71 |
72 |
73 |
74 | 75 | 76 | -------------------------------------------------------------------------------- /templates/stats.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | TwitFix Stats 4 | 7 | 8 | 37 | 38 | 39 |
40 |
41 |
42 | 43 | 44 | -------------------------------------------------------------------------------- /templates/text.html: -------------------------------------------------------------------------------- 1 | {% extends 'base.html' %} 2 | 20 | {% block head %} 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | {% endblock %} 36 | 37 | {% block body %} 38 | Redirecting you to the tweet in a moment. Or click here. 39 | {% endblock %} 40 | -------------------------------------------------------------------------------- /templates/video.html: -------------------------------------------------------------------------------- 1 | {% extends 'base.html' %} 2 | 3 | {% block head %} 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | {% endblock %} 28 | 29 | {% block body %} 30 | Redirecting you to the tweet in a moment. Or click here. 31 | {% endblock %} 32 | -------------------------------------------------------------------------------- /twitfix.ini: -------------------------------------------------------------------------------- 1 | [uwsgi] 2 | module = wsgi:app 3 | 4 | master = true 5 | processes = 5 6 | 7 | socket = twitfix.sock 8 | chmod-socket = 660 9 | vacuum = true 10 | 11 | die-on-term = true -------------------------------------------------------------------------------- /twitfix.py: -------------------------------------------------------------------------------- 1 | from flask import Flask, render_template, request, redirect, Response, send_from_directory, url_for, send_file, make_response, jsonify 2 | from flask_cors import CORS 3 | import youtube_dl 4 | import textwrap 5 | import twitter 6 | import pymongo 7 | import requests 8 | import json 9 | import re 10 | import os 11 | import urllib.parse 12 | import urllib.request 13 | from datetime import date 14 | 15 | app = Flask(__name__) 16 | CORS(app) 17 | 18 | pathregex = re.compile("\\w{1,15}\\/(status|statuses)\\/\\d{2,20}") 19 | generate_embed_user_agents = [ 20 | "facebookexternalhit/1.1", 21 | "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36", 22 | "Mozilla/5.0 (Windows; U; Windows NT 10.0; en-US; Valve Steam Client/default/1596241936; ) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36", 23 | "Mozilla/5.0 (Windows; U; Windows NT 10.0; en-US; Valve Steam Client/default/0; ) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36", 24 | "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_1) AppleWebKit/601.2.4 (KHTML, like Gecko) Version/9.0.1 Safari/601.2.4 facebookexternalhit/1.1 Facebot Twitterbot/1.0", 25 | "facebookexternalhit/1.1", 26 | "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; Valve Steam FriendsUI Tenfoot/0; ) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36", 27 | "Slackbot-LinkExpanding 1.0 (+https://api.slack.com/robots)", 28 | "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:38.0) Gecko/20100101 Firefox/38.0", 29 | "Mozilla/5.0 (compatible; Discordbot/2.0; +https://discordapp.com)", 30 | "TelegramBot (like TwitterBot)", 31 | "Mozilla/5.0 (compatible; January/1.0; +https://gitlab.insrt.uk/revolt/january)", 32 | "test"] 33 | 34 | # Read config from config.json. If it does not exist, create new. 35 | if not os.path.exists("config.json"): 36 | with open("config.json", "w") as outfile: 37 | default_config = { 38 | "config":{ 39 | "link_cache":"json", 40 | "database":"[url to mongo database goes here]", 41 | "table":"TwiFix", 42 | "method":"youtube-dl", 43 | "color":"#43B581", 44 | "appname": "TwitFix", 45 | "repo": "https://github.com/robinuniverse/twitfix", 46 | "url": "https://fxtwitter.com" 47 | }, 48 | "api":{"api_key":"[api_key goes here]", 49 | "api_secret":"[api_secret goes here]", 50 | "access_token":"[access_token goes here]", 51 | "access_secret":"[access_secret goes here]" 52 | } 53 | } 54 | 55 | json.dump(default_config, outfile, indent=4, sort_keys=True) 56 | 57 | config = default_config 58 | else: 59 | f = open("config.json") 60 | config = json.load(f) 61 | f.close() 62 | 63 | # If method is set to API or Hybrid, attempt to auth with the Twitter API 64 | if config['config']['method'] in ('api', 'hybrid'): 65 | auth = twitter.oauth.OAuth(config['api']['access_token'], config['api']['access_secret'], config['api']['api_key'], config['api']['api_secret']) 66 | twitter_api = twitter.Twitter(auth=auth) 67 | 68 | link_cache_system = config['config']['link_cache'] 69 | 70 | if link_cache_system == "json": 71 | link_cache = {} 72 | if not os.path.exists("config.json"): 73 | with open("config.json", "w") as outfile: 74 | default_link_cache = {"test":"test"} 75 | json.dump(default_link_cache, outfile, indent=4, sort_keys=True) 76 | 77 | f = open('links.json',) 78 | link_cache = json.load(f) 79 | f.close() 80 | elif link_cache_system == "db": 81 | client = pymongo.MongoClient(config['config']['database'], connect=False) 82 | table = config['config']['table'] 83 | db = client[table] 84 | 85 | @app.route('/bidoof/') 86 | def bidoof(): 87 | return redirect("https://cdn.discordapp.com/attachments/291764448757284885/937343686927319111/IMG_20211226_202956_163.webp", 301) 88 | 89 | @app.route('/discord/') 90 | def discord(): 91 | return redirect("https://discord.gg/ztz2hHwZXv", 301) 92 | 93 | @app.route('/stats/') 94 | def statsPage(): 95 | today = str(date.today()) 96 | stats = getStats(today) 97 | return render_template('stats.html', embeds=stats['embeds'], downloadss=stats['downloads'], api=stats['api'], linksCached=stats['linksCached'], date=today) 98 | 99 | @app.route('/latest/') 100 | def latest(): 101 | return render_template('latest.html') 102 | 103 | @app.route('/copy.svg') # Return a SVG needed for Latest 104 | def icon(): 105 | return send_from_directory(os.path.join(app.root_path, 'static'), 106 | 'copy.svg',mimetype='image/svg+xml') 107 | 108 | @app.route('/font.ttf') # Return a font needed for Latest 109 | def font(): 110 | return send_from_directory(os.path.join(app.root_path, 'static'), 111 | 'NotoColorEmoji.ttf',mimetype='application/octet-stream') 112 | 113 | @app.route('/top/') # Try to return the most hit video 114 | def top(): 115 | vnf = db.linkCache.find_one(sort = [('hits', pymongo.DESCENDING)]) 116 | desc = re.sub(r' http.*t\.co\S+', '', vnf['description']) 117 | urlUser = urllib.parse.quote(vnf['uploader']) 118 | urlDesc = urllib.parse.quote(desc) 119 | urlLink = urllib.parse.quote(vnf['url']) 120 | print(" ➤ [ ✔ ] Top video page loaded: " + vnf['tweet'] ) 121 | return render_template('inline.html', page="Top", vidlink=vnf['url'], vidurl=vnf['url'], desc=desc, pic=vnf['thumbnail'], user=vnf['uploader'], video_link=vnf['url'], color=config['config']['color'], appname=config['config']['appname'], repo=config['config']['repo'], url=config['config']['url'], urlDesc=urlDesc, urlUser=urlUser, urlLink=urlLink, tweet=vnf['tweet']) 122 | 123 | @app.route('/api/latest/') # Return some raw VNF data sorted by top tweets 124 | def apiLatest(): 125 | bigvnf = [] 126 | 127 | tweets = request.args.get("tweets", default=10, type=int) 128 | page = request.args.get("page", default=0, type=int) 129 | 130 | if tweets > 15: 131 | tweets = 1 132 | 133 | vnf = db.linkCache.find(sort = [('_id', pymongo.DESCENDING)]).skip(tweets * page).limit(tweets) 134 | 135 | for r in vnf: 136 | bigvnf.append(r) 137 | 138 | print(" ➤ [ ✔ ] Latest video API called") 139 | addToStat('api') 140 | return Response(response=json.dumps(bigvnf, default=str), status=200, mimetype="application/json") 141 | 142 | @app.route('/api/top/') # Return some raw VNF data sorted by top tweets 143 | def apiTop(): 144 | bigvnf = [] 145 | 146 | tweets = request.args.get("tweets", default=10, type=int) 147 | page = request.args.get("page", default=0, type=int) 148 | 149 | if tweets > 15: 150 | tweets = 1 151 | 152 | vnf = db.linkCache.find(sort = [('hits', pymongo.DESCENDING )]).skip(tweets * page).limit(tweets) 153 | 154 | for r in vnf: 155 | bigvnf.append(r) 156 | 157 | print(" ➤ [ ✔ ] Top video API called") 158 | addToStat('api') 159 | return Response(response=json.dumps(bigvnf, default=str), status=200, mimetype="application/json") 160 | 161 | @app.route('/api/stats/') # Return a json of a usage stats for a given date (defaults to today) 162 | def apiStats(): 163 | try: 164 | addToStat('api') 165 | today = str(date.today()) 166 | desiredDate = request.args.get("date", default=today, type=str) 167 | stat = getStats(desiredDate) 168 | print (" ➤ [ ✔ ] Stats API called") 169 | return Response(response=json.dumps(stat, default=str), status=200, mimetype="application/json") 170 | except: 171 | print (" ➤ [ ✔ ] Stats API failed") 172 | 173 | @app.route('/') # If the useragent is discord, return the embed, if not, redirect to configured repo directly 174 | def default(): 175 | user_agent = request.headers.get('user-agent') 176 | if user_agent in generate_embed_user_agents: 177 | return message("TwitFix is an attempt to fix twitter video embeds in discord! created by Robin Universe :)\n\n💖\n\nClick me to be redirected to the repo!") 178 | else: 179 | return redirect(config['config']['repo'], 301) 180 | 181 | @app.route('/oembed.json') #oEmbed endpoint 182 | def oembedend(): 183 | desc = request.args.get("desc", None) 184 | user = request.args.get("user", None) 185 | link = request.args.get("link", None) 186 | ttype = request.args.get("ttype", None) 187 | return oEmbedGen(desc, user, link, ttype) 188 | 189 | @app.route('/') # Default endpoint used by everything 190 | def twitfix(sub_path): 191 | user_agent = request.headers.get('user-agent') 192 | match = pathregex.search(sub_path) 193 | print(request.url) 194 | 195 | if request.url.startswith("https://d.fx"): # Matches d.fx? Try to give the user a direct link 196 | if user_agent in generate_embed_user_agents: 197 | print( " ➤ [ D ] d.fx link shown to discord user-agent!") 198 | if request.url.endswith(".mp4") and "?" not in request.url: 199 | return dl(sub_path) 200 | else: 201 | return message("To use a direct MP4 link in discord, remove anything past '?' and put '.mp4' at the end") 202 | else: 203 | print(" ➤ [ R ] Redirect to MP4 using d.fxtwitter.com") 204 | return dir(sub_path) 205 | 206 | elif request.url.endswith(".mp4") or request.url.endswith("%2Emp4"): 207 | twitter_url = "https://twitter.com/" + sub_path 208 | 209 | if "?" not in request.url: 210 | clean = twitter_url[:-4] 211 | else: 212 | clean = twitter_url 213 | 214 | return dl(clean) 215 | 216 | elif request.url.endswith(".json") or request.url.endswith("%2Ejson"): 217 | twitter_url = "https://twitter.com/" + sub_path 218 | 219 | if "?" not in request.url: 220 | clean = twitter_url[:-5] 221 | else: 222 | clean = twitter_url 223 | 224 | print( " ➤ [ API ] VNF Json api hit!") 225 | 226 | vnf = link_to_vnf_from_api(clean.replace(".json","")) 227 | 228 | if user_agent in generate_embed_user_agents: 229 | return message("VNF Data: ( discord useragent preview )\n\n"+ json.dumps(vnf, default=str)) 230 | else: 231 | return Response(response=json.dumps(vnf, default=str), status=200, mimetype="application/json") 232 | 233 | elif request.url.endswith("/1") or request.url.endswith("/2") or request.url.endswith("/3") or request.url.endswith("/4") or request.url.endswith("%2F1") or request.url.endswith("%2F2") or request.url.endswith("%2F3") or request.url.endswith("%2F4"): 234 | twitter_url = "https://twitter.com/" + sub_path 235 | 236 | if "?" not in request.url: 237 | clean = twitter_url[:-2] 238 | else: 239 | clean = twitter_url 240 | 241 | image = ( int(request.url[-1]) - 1 ) 242 | return embed_video(clean, image) 243 | 244 | if match is not None: 245 | twitter_url = sub_path 246 | 247 | if match.start() == 0: 248 | twitter_url = "https://twitter.com/" + sub_path 249 | 250 | if user_agent in generate_embed_user_agents: 251 | res = embed_video(twitter_url) 252 | return res 253 | 254 | else: 255 | print(" ➤ [ R ] Redirect to " + twitter_url) 256 | return redirect(twitter_url, 301) 257 | else: 258 | return message("This doesn't appear to be a twitter URL") 259 | 260 | @app.route('/other/') # Show all info that Youtube-DL can get about a video as a json 261 | def other(sub_path): 262 | otherurl = request.url.split("/other/", 1)[1].replace(":/","://") 263 | print(" ➤ [ OTHER ] Other URL embed attempted: " + otherurl) 264 | res = embed_video(otherurl) 265 | return res 266 | 267 | @app.route('/info/') # Show all info that Youtube-DL can get about a video as a json 268 | def info(sub_path): 269 | infourl = request.url.split("/info/", 1)[1].replace(":/","://") 270 | print(" ➤ [ INFO ] Info data requested: " + infourl) 271 | with youtube_dl.YoutubeDL({'outtmpl': '%(id)s.%(ext)s'}) as ydl: 272 | result = ydl.extract_info(infourl, download=False) 273 | 274 | return result 275 | 276 | @app.route('/dl/') # Download the tweets video, and rehost it 277 | def dl(sub_path): 278 | print(' ➤ [[ !!! TRYING TO DOWNLOAD FILE !!! ]] Downloading file from ' + sub_path) 279 | url = sub_path 280 | match = pathregex.search(url) 281 | if match is not None: 282 | twitter_url = url 283 | if match.start() == 0: 284 | twitter_url = "https://twitter.com/" + url 285 | 286 | mp4link = direct_video_link(twitter_url) 287 | filename = (sub_path.split('/')[-1].split('.mp4')[0] + '.mp4') 288 | 289 | PATH = ( './static/' + filename ) 290 | if os.path.isfile(PATH) and os.access(PATH, os.R_OK): 291 | print(" ➤ [[ FILE EXISTS ]]") 292 | else: 293 | print(" ➤ [[ FILE DOES NOT EXIST, DOWNLOADING... ]]") 294 | addToStat('downloads') 295 | mp4file = urllib.request.urlopen(mp4link) 296 | with open(('/home/robin/twitfix/static/' + filename), 'wb') as output: 297 | output.write(mp4file.read()) 298 | 299 | print(' ➤ [[ PRESENTING FILE: '+ filename +', URL: https://fxtwitter.com/static/'+ filename +' ]]') 300 | r = make_response(send_file(('static/' + filename), mimetype='video/mp4', max_age=100)) 301 | r.headers['Content-Type'] = 'video/mp4' 302 | r.headers['Sec-Fetch-Site'] = 'none' 303 | r.headers['Sec-Fetch-User'] = '?1' 304 | return r 305 | 306 | @app.route('/dir/') # Try to return a direct link to the MP4 on twitters servers 307 | def dir(sub_path): 308 | user_agent = request.headers.get('user-agent') 309 | url = sub_path 310 | match = pathregex.search(url) 311 | if match is not None: 312 | twitter_url = url 313 | 314 | if match.start() == 0: 315 | twitter_url = "https://twitter.com/" + url 316 | 317 | if user_agent in generate_embed_user_agents: 318 | res = embed_video(twitter_url) 319 | return res 320 | 321 | else: 322 | print(" ➤ [ R ] Redirect to direct MP4 URL") 323 | return direct_video(twitter_url) 324 | else: 325 | return redirect(url, 301) 326 | 327 | @app.route('/favicon.ico') # This shit don't work 328 | def favicon(): 329 | return send_from_directory(os.path.join(app.root_path, 'static'), 330 | 'favicon.ico',mimetype='image/vnd.microsoft.icon') 331 | 332 | def direct_video(video_link): # Just get a redirect to a MP4 link from any tweet link 333 | cached_vnf = getVnfFromLinkCache(video_link) 334 | if cached_vnf == None: 335 | try: 336 | vnf = link_to_vnf(video_link) 337 | addVnfToLinkCache(video_link, vnf) 338 | return redirect(vnf['url'], 301) 339 | print(" ➤ [ D ] Redirecting to direct URL: " + vnf['url']) 340 | except Exception as e: 341 | print(e) 342 | return message("Failed to scan your link!") 343 | else: 344 | return redirect(cached_vnf['url'], 301) 345 | print(" ➤ [ D ] Redirecting to direct URL: " + vnf['url']) 346 | 347 | def direct_video_link(video_link): # Just get a redirect to a MP4 link from any tweet link 348 | cached_vnf = getVnfFromLinkCache(video_link) 349 | if cached_vnf == None: 350 | try: 351 | vnf = link_to_vnf(video_link) 352 | addVnfToLinkCache(video_link, vnf) 353 | return vnf['url'] 354 | print(" ➤ [ D ] Redirecting to direct URL: " + vnf['url']) 355 | except Exception as e: 356 | print(e) 357 | return message("Failed to scan your link!") 358 | else: 359 | return cached_vnf['url'] 360 | print(" ➤ [ D ] Redirecting to direct URL: " + vnf['url']) 361 | 362 | def addToStat(stat): 363 | #print(stat) 364 | today = str(date.today()) 365 | try: 366 | collection = db.stats.find_one({'date': today}) 367 | delta = ( collection[stat] + 1 ) 368 | query = { "date" : today } 369 | change = { "$set" : { stat : delta } } 370 | out = db.stats.update_one(query, change) 371 | except: 372 | collection = db.stats.insert_one({'date': today, "embeds" : 1, "linksCached" : 1, "api" : 1, "downloads" : 1 }) 373 | 374 | 375 | def getStats(day): 376 | collection = db.stats.find_one({'date': day}) 377 | return collection 378 | 379 | def embed_video(video_link, image=0): # Return Embed from any tweet link 380 | cached_vnf = getVnfFromLinkCache(video_link) 381 | 382 | if cached_vnf == None: 383 | try: 384 | vnf = link_to_vnf(video_link) 385 | addVnfToLinkCache(video_link, vnf) 386 | return embed(video_link, vnf, image) 387 | 388 | except Exception as e: 389 | print(e) 390 | return message("Failed to scan your link!") 391 | else: 392 | return embed(video_link, cached_vnf, image) 393 | 394 | def tweetInfo(url, tweet="", desc="", thumb="", uploader="", screen_name="", pfp="", tweetType="", images="", hits=0, likes=0, rts=0, time="", qrt={}, nsfw=False): # Return a dict of video info with default values 395 | vnf = { 396 | "tweet" : tweet, 397 | "url" : url, 398 | "description" : desc, 399 | "thumbnail" : thumb, 400 | "uploader" : uploader, 401 | "screen_name" : screen_name, 402 | "pfp" : pfp, 403 | "type" : tweetType, 404 | "images" : images, 405 | "hits" : hits, 406 | "likes" : likes, 407 | "rts" : rts, 408 | "time" : time, 409 | "qrt" : qrt, 410 | "nsfw" : nsfw 411 | } 412 | return vnf 413 | 414 | def link_to_vnf_from_api(video_link): 415 | print(" ➤ [ + ] Attempting to download tweet info from Twitter API") 416 | twid = int(re.sub(r'\?.*$','',video_link.rsplit("/", 1)[-1])) # gets the tweet ID as a int from the passed url 417 | tweet = twitter_api.statuses.show(_id=twid, tweet_mode="extended") 418 | # For when I need to poke around and see what a tweet looks like 419 | #print(tweet) 420 | imgs = ["","","","", ""] 421 | print(" ➤ [ + ] Tweet Type: " + tweetType(tweet)) 422 | # Check to see if tweet has a video, if not, make the url passed to the VNF the first t.co link in the tweet 423 | if tweetType(tweet) == "Video": 424 | if tweet['extended_entities']['media'][0]['video_info']['variants']: 425 | best_bitrate = 0 426 | thumb = tweet['extended_entities']['media'][0]['media_url'] 427 | for video in tweet['extended_entities']['media'][0]['video_info']['variants']: 428 | if video['content_type'] == "video/mp4" and video['bitrate'] > best_bitrate: 429 | url = video['url'] 430 | elif tweetType(tweet) == "Text": 431 | url = "" 432 | thumb = "" 433 | else: 434 | imgs = ["","","","", ""] 435 | i = 0 436 | for media in tweet['extended_entities']['media']: 437 | imgs[i] = media['media_url_https'] 438 | i = i + 1 439 | 440 | #print(imgs) 441 | imgs[4] = str(i) 442 | url = "" 443 | images= imgs 444 | thumb = tweet['extended_entities']['media'][0]['media_url_https'] 445 | 446 | qrt = {} 447 | 448 | if 'quoted_status' in tweet: 449 | qrt['desc'] = tweet['quoted_status']['full_text'] 450 | qrt['handle'] = tweet['quoted_status']['user']['name'] 451 | qrt['screen_name'] = tweet['quoted_status']['user']['screen_name'] 452 | 453 | text = tweet['full_text'] 454 | 455 | if 'possibly_sensitive' in tweet: 456 | nsfw = tweet['possibly_sensitive'] 457 | else: 458 | nsfw = False 459 | 460 | vnf = tweetInfo( 461 | url, 462 | video_link, 463 | text, thumb, 464 | tweet['user']['name'], 465 | tweet['user']['screen_name'], 466 | tweet['user']['profile_image_url'], 467 | tweetType(tweet), 468 | likes=tweet['favorite_count'], 469 | rts=tweet['retweet_count'], 470 | time=tweet['created_at'], 471 | qrt=qrt, 472 | images=imgs, 473 | nsfw=nsfw 474 | ) 475 | 476 | return vnf 477 | 478 | def link_to_vnf_from_youtubedl(video_link): 479 | print(" ➤ [ X ] Attempting to download tweet info via YoutubeDL: " + video_link) 480 | with youtube_dl.YoutubeDL({'outtmpl': '%(id)s.%(ext)s'}) as ydl: 481 | result = ydl.extract_info(video_link, download=False) 482 | vnf = tweetInfo(result['url'], video_link, result['description'].rsplit(' ',1)[0], result['thumbnail'], result['uploader']) 483 | return vnf 484 | 485 | def link_to_vnf(video_link): # Return a VideoInfo object or die trying 486 | if config['config']['method'] == 'hybrid': 487 | try: 488 | return link_to_vnf_from_api(video_link) 489 | except Exception as e: 490 | print(" ➤ [ !!! ] API Failed") 491 | print(e) 492 | return link_to_vnf_from_youtubedl(video_link) 493 | elif config['config']['method'] == 'api': 494 | try: 495 | return link_to_vnf_from_api(video_link) 496 | except Exception as e: 497 | print(" ➤ [ X ] API Failed") 498 | print(e) 499 | return None 500 | elif config['config']['method'] == 'youtube-dl': 501 | try: 502 | return link_to_vnf_from_youtubedl(video_link) 503 | except Exception as e: 504 | print(" ➤ [ X ] Youtube-DL Failed") 505 | print(e) 506 | return None 507 | else: 508 | print("Please set the method key in your config file to 'api' 'youtube-dl' or 'hybrid'") 509 | return None 510 | 511 | def getVnfFromLinkCache(video_link): 512 | if link_cache_system == "db": 513 | collection = db.linkCache 514 | vnf = collection.find_one({'tweet': video_link}) 515 | # print(vnf) 516 | if vnf != None: 517 | hits = ( vnf['hits'] + 1 ) 518 | print(" ➤ [ ✔ ] Link located in DB cache. " + "hits on this link so far: [" + str(hits) + "]") 519 | query = { 'tweet': video_link } 520 | change = { "$set" : { "hits" : hits } } 521 | out = db.linkCache.update_one(query, change) 522 | addToStat('embeds') 523 | return vnf 524 | else: 525 | print(" ➤ [ X ] Link not in DB cache") 526 | return None 527 | elif link_cache_system == "json": 528 | if video_link in link_cache: 529 | print("Link located in json cache") 530 | vnf = link_cache[video_link] 531 | return vnf 532 | else: 533 | print(" ➤ [ X ] Link not in json cache") 534 | return None 535 | 536 | def addVnfToLinkCache(video_link, vnf): 537 | if link_cache_system == "db": 538 | try: 539 | out = db.linkCache.insert_one(vnf) 540 | print(" ➤ [ + ] Link added to DB cache ") 541 | addToStat('linksCached') 542 | return True 543 | except Exception: 544 | print(" ➤ [ X ] Failed to add link to DB cache") 545 | return None 546 | elif link_cache_system == "json": 547 | link_cache[video_link] = vnf 548 | with open("links.json", "w") as outfile: 549 | json.dump(link_cache, outfile, indent=4, sort_keys=True) 550 | return None 551 | 552 | def message(text): 553 | return render_template( 554 | 'default.html', 555 | message = text, 556 | color = config['config']['color'], 557 | appname = config['config']['appname'], 558 | repo = config['config']['repo'], 559 | url = config['config']['url'] ) 560 | 561 | def embed(video_link, vnf, image): 562 | print(" ➤ [ E ] Embedding " + vnf['type'] + ": " + vnf['url']) 563 | 564 | desc = re.sub(r' http.*t\.co\S+', '', vnf['description']) 565 | urlUser = urllib.parse.quote(vnf['uploader']) 566 | urlDesc = urllib.parse.quote(desc) 567 | urlLink = urllib.parse.quote(video_link) 568 | likeDisplay = ("\n\n💖 " + str(vnf['likes']) + " 🔁 " + str(vnf['rts']) + "\n") 569 | 570 | try: 571 | if vnf['type'] == "": 572 | desc = desc 573 | elif vnf['type'] == "Video": 574 | desc = desc 575 | elif vnf['qrt'] == {}: # Check if this is a QRT and modify the description 576 | desc = (desc + likeDisplay) 577 | else: 578 | qrtDisplay = ("\n─────────────\n ➤ QRT of " + vnf['qrt']['handle'] + " (@" + vnf['qrt']['screen_name'] + "):\n─────────────\n'" + vnf['qrt']['desc'] + "'") 579 | desc = (desc + qrtDisplay + likeDisplay) 580 | except: 581 | vnf['likes'] = 0; vnf['rts'] = 0; vnf['time'] = 0 582 | print(' ➤ [ X ] Failed QRT check - old VNF object') 583 | 584 | if vnf['type'] == "Text": # Change the template based on tweet type 585 | template = 'text.html' 586 | if vnf['type'] == "Image": 587 | image = vnf['images'][image] 588 | template = 'image.html' 589 | if vnf['type'] == "Video": 590 | urlDesc = urllib.parse.quote(textwrap.shorten(desc, width=220, placeholder="...")) 591 | template = 'video.html' 592 | if vnf['type'] == "": 593 | urlDesc = urllib.parse.quote(textwrap.shorten(desc, width=220, placeholder="...")) 594 | template = 'video.html' 595 | 596 | color = "#7FFFD4" # Green 597 | 598 | if vnf['nsfw'] == True: 599 | color = "#800020" # Red 600 | 601 | return render_template( 602 | template, 603 | likes = vnf['likes'], 604 | rts = vnf['rts'], 605 | time = vnf['time'], 606 | screenName = vnf['screen_name'], 607 | vidlink = vnf['url'], 608 | pfp = vnf['pfp'], 609 | vidurl = vnf['url'], 610 | desc = desc, 611 | pic = image, 612 | user = vnf['uploader'], 613 | video_link = video_link, 614 | color = color, 615 | appname = config['config']['appname'], 616 | repo = config['config']['repo'], 617 | url = config['config']['url'], 618 | urlDesc = urlDesc, 619 | urlUser = urlUser, 620 | urlLink = urlLink ) 621 | 622 | def tweetType(tweet): # Are we dealing with a Video, Image, or Text tweet? 623 | if 'extended_entities' in tweet: 624 | if 'video_info' in tweet['extended_entities']['media'][0]: 625 | out = "Video" 626 | else: 627 | out = "Image" 628 | else: 629 | out = "Text" 630 | 631 | return out 632 | 633 | 634 | def oEmbedGen(description, user, video_link, ttype): 635 | out = { 636 | "type" : ttype, 637 | "version" : "1.0", 638 | "provider_name" : config['config']['appname'], 639 | "provider_url" : config['config']['repo'], 640 | "title" : description, 641 | "author_name" : user, 642 | "author_url" : video_link 643 | } 644 | 645 | return out 646 | 647 | if __name__ == "__main__": 648 | app.config['SERVER_NAME']='localhost:80' 649 | app.run(host='0.0.0.0') 650 | -------------------------------------------------------------------------------- /twitfix.service: -------------------------------------------------------------------------------- 1 | [Unit] 2 | Description=Init file for twitfix uwsgi instance 3 | After=network.target 4 | 5 | [Service] 6 | User=robin 7 | Group=robin 8 | WorkingDirectory=/home/robin/twitfix 9 | Environment="PATH=/home/robin/twitfix/twitfixenv/bin" 10 | ExecStart=/home/robin/twitfix/twitfixenv/bin/uwsgi --ini twitfix.ini 11 | 12 | [Install] 13 | WantedBy=multi-user.target -------------------------------------------------------------------------------- /wsgi.py: -------------------------------------------------------------------------------- 1 | from twitfix import app 2 | 3 | if __name__ == "__main__": 4 | # listen on 0.0.0.0 to facilitate testing with real services 5 | app.run(host='0.0.0.0') --------------------------------------------------------------------------------