I think everyone remembers the telnet version of SW: New Hope. In connection with the blocking of PornHub, an idea arose to do something similar with their video. Armed with a python and try to implement the idea.
That StarWars.
1. Content extraction
Pornhab has quite a distinct api, so let's use it. The description is here (unfortunately, you will have to register). With it, you can pull out links to pages with video, but then we still have to dig into html. Fortunately, the desired link is easily ripped out using the regular page:
What else is worth noting at this stage: pornhub periodically returns 403 when trying to download a video, this problem is solved by changing the User-Agent. ')
2. Conversion
Now we need to convert the video to ASCII graphics. To do this, we use OpenCV. The algorithm is extremely simple:
RGB -> Grayscale
Downscale to the desired resolution
Convert pixels to character
Since we are going to output the result in telnet, let's take the 80x24 resolution as a basis:
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) res = cv2.resize(gray, (80, 24))
I think we should explain why we are converting to grayscale: we are only interested in information about the illumination, it can be obtained in two ways - convert to YUV and pull out the Y channel or convert to grayscale.
Then we just need to iterate over the pixels and convert the resulting values into characters:
deflumToChar(l): result = '@'if l >= 230.0: result = ' 'elif l >= 200.0: result = '.'elif l >= 180.0: result = '*'elif l >= 160.0: result = ':'elif l >= 130.0: result = 'o'elif l >= 100.0: result = '&'elif l >= 70.0: result = '8'elif l >= 50.0: result = '#'return result
3. Output to telnet
All that is left for us to do is to raise the tcp server and stream our video there when connected. However, there is a snag: every frame needs to be cleared buffer. To do this, simply send the client an escape sequence:
escapeSeq = '\033[2J'
NSFW
Guess what it is.
4. Conclusion
Q: Why? A: There was free time. Q: Where can I get results? A: telnet 185.146.170.134 9002