AnnouncementsMatrixEventsFunnyVideosMusicAncapsTechnologyEconomicsPrivacyGIFSCringeAnarchyFilmPicsThemesIdeas4MatrixAskMatrixHelpTop Subs
3
-module(catbox).
-export([init/0,upload/1,transfer/1,transfer_loop/0]).

-define(URL,"https://catbox.moe/user/api.php").
-define(USERAGENT,"depthbomb/node-catbox").
-define(Headers,[{<<"User-Agent">>,?USERAGENT}]).
-define(Options,[]).

init()->
 application:ensure_all_started(hackney).

formBody(Path)->
 PathBin = list_to_binary(Path),
 ReqTypePart = {<<"reqtype">>,<<"fileupload">>},
 %FilePart = {<<"fileToUpload">>,{file,Path}},
 FilePart = {file,PathBin,<<"fileToUpload">>,[]},
 Parts = [ReqTypePart,FilePart],
 Body = {multipart, Parts},
 Body.


upload(Path)->
 {ok, _ , _ , ClientRef} = hackney:post(?URL,?Headers,formBody(Path),?Options),
 {ok, Bin} = hackney:body(ClientRef),
 Result = binary_to_list(Bin),
 Result.

randpath()->"/tmp/"++binary_to_list(base64:encode(crypto:strong_rand_bytes(12)))++".mp4".

transfer(Url)->
 Tmp = randpath(),
 io:format("Temp path: ~p~n",[Tmp]),
 Cmd = "yt-dlp '"++Url++"' -o '"++Tmp++"'",
 io:format("Cmd: ~p~n",[Cmd]),
 os:cmd(Cmd),
 %io:format("Shell: ~p~n",[Shell]),
 NewURL = upload(Tmp),
 NewURL.
Comment preview
[-]JasonCarswell
1(+1|0)

Please explain like I'm five.

[-]x0x7
0(0|0)

It uses yt-dlp to download the video. It then uploads the file to catbox as a file form. It uses a programming language that is the highest paying language to know right now. So obviously I want excuses to code in it so I can learn it better.

Being able to write this and being a professional developer with it are two completely different levels.

Though TBH I've tutored women who needed tutoring past getting a job in basic python shit while not actually knowing python myself. Their python was that bad that I was able to be a substantial help. So if you are a woman what I wrote above is enough to get a job.

Hmm. If I need one more project to do I should make based linkedin, where writing something like this is acceptable.

[-]x0x7
0(0|0)

Long explanation going into code so the code makes sense.

-module declares what module we're writing.
-export says what functions other code can access.
The /0 or /1 is saying how many arguments the function takes, called the arity in functional programming languages.
-define is just defining some constants so they don't clutter the code.

Functions are defined with name(arguments) -> implementation.
All functions return one value.

Erlang uses sentencs with normal sentence punctuation. Very unusual for most languages. The end of a statement or function declaration ends with a period. Assignments within a function can be split up with commas. Between commas and periods there are semicolons that split up clauses. But this code has no semi-colons. Nice.

We are using a library called hackney that does http requests.

The init function just makes sure everything hackney depends on is started.

The main thing it needs is the net module's server. Erlang often splits code into separate processes because it is a distributed programming language, which is the core strength that makes it pay so well. Sometimes modules split off a single (often single) process to handle some functions in. Net is one of those.

The formBody function takes a file path and makes the bytes that will make of the body of an http request in the format Catbox expects.

The upload function is similar but a little more broad. It actually sends the request. Sometimes this is called a wrapper function because it calls formBody and does more.

The transfer function is a function I added later. It's a wrapper function to the upload function. The doing more is downloading a video file with yt-dlp.

Last concept hint that can make reading this code easier is understanding that Erlang is a pattern matching language with destructuring. The lines like {ok, Bin} = hackney:body(clientRef), basically takes the result from that function call and breaks it up if and only if the response matches that pattern. Variables are always uppercase. So Bin in this case. If it is lowercase then it is an atom which are kind of like enumerators in other languages, but they don't have to be declared. They are also sort of like string constants except in their own space so text data and constants don't cross paths. Curly braces {} define a tuple which is like a fixed length list. So the line {ok, Bin} = hackney:body(ClientRef) assigns the second part of the result to Bin if and only if the first part of the result equals the atom ok.

Underscore is a common notation for a variable we don't care about. So {ok, _ , _ , ClientRef} = SomeValue. Just doesn't assign the second and third value. Or at least assigns them into a space that who the fuck cares.

The syntax <<"string">> vs "string" is because Hackney is coded in Elixir a cousin language and it tends to use binary strings instead of list strings so we have to convert between them. This also explains the use of binary_to_list and list_to_binary functions.

Now you understand the code.