As Jay pointed out you should look at the source code.
‘Onebox’ is the library that, in part, converts some URLs into embeds.
Have you looked at?:
I think the original intention was that this code be also used on non-Discourse sites.
To that end there’s a (now unmaintained) gem that you might be able to leverage (assuming your site is RoR), maybe you’d be interested in maintaining it?
https://github.com/discourse/onebox
A word of caution: the code on Discourse will have positively evolved since.