Showing posts with label automate. Show all posts
Showing posts with label automate. Show all posts

Sunday, 15 April 2018

Duh - Saves you the trouble to correct your command


Duh.

This is no more an expression for me but a command now. Thanks to the hack I have been doing for past couple of days.

What's it about? Well, here it goes.

How many times it happens that we screw up commands on terminal?
A typo, a syntax mistake or jumbled up arguments. The command doesn't run and then we spend time retyping it ensuring everything is in place this time.
Quite time consuming, eh?




My laziness simply denied me such a behaviour. So I coded up a powershell cmdlet which can do this for me.
Now if I mess up a command, I just have to type 'Duh' and the right command will be displayed on the prompt for you to check and execute (press ENTER).

How Duh operates internally?
Well, guess what. Answer lies in the "tries".
We have a trie and we do closest match using Leveinshtein Distance.
In short, how to figure out how close two strings are?  Find the no of letters you need to remove/insert/replace in order to attain string 2 from string 1.
This is what's being used in finding the closest correct command to the input command.
Hey but wait, what are the right set of commands constituting the tries? Where do we get these from?
For now I am using two things:
1.  A list of standard git commands (with placeholders for params)
scraped from a website.
2. The history of currently opened powershell.
Why to use the history of commands on a powershell you may ask.
Here's the catch, while in a session there are a lot of commands that we use and reuse , using the latest history of powershell commands will help us correct the cases where we mess up a recently pre-used command (along with right set of params values) with ease since they exist in trie.

How to get history? Use "get-history". Lol.
But this command gives me history of all the commands typed on powershell, including the wrong ones. And we for sure don't want to put them in the trie. After all the programs is not: "We will return another wrong command in exchange with the wrong command you wrote, well just because it's chic, also beauty lies in imperfection".

There's a need to filter out these commands and pick only the right ones.
How to figure out which ones are right from the dumped output? I thought about drawing some heuristics from sample data.
Is there a pattern in execution time of failed commands? Is the time fixed? Or is it the least taken?
Stupid questions as I think of them now. There were definitely commands that were much faster to execute. Also the logic that fails the execution of any command is not the same and takes it's own time. Most importantly I believe the execution time of the same command may also differ based on how much is your CPU occupied.
So, how to filter out? Presently I am using the assumption that the wrong commands are more likely to be present in infrequent numbers. Especially because the chances of repeating a command wrongly in the same manner are really low. Well this is not very definitive and for sure there lies a good amount of drawbacks in this approach : we might lose out on many of the potentially correct commands and also, in case the wrong command is repeated again we will add it to trie and offer it as a suggestion the next time- a totally corrupted experience.
But well this is what I picked up for now.
There's one more better approach where we maintain a map of standard commands with their params and then find the closest match , this support will also help us in handling flags and new params with grace. So will be adding this up soon.

So that is what runs in the cmdlet code ( in c#).
For using this command you need to import the dll as a module and also set the alias duh for ease of usage. I have added a powershell profile.ps1 to ease that up.
We need to place that file in systems32 powershell directory (C:\Windows\System32\WindowsPowerShell\v1.0) and we should be all set.

Haah, so that's what the hack was all about.
I did it for powershell only, for now since I am using that these days but the code can be extended to operate with the other shells too, essentially the IO logic will change.
If you get interested, you can check out the source code at:
https://github.com/nextLane/Duh

There are a lot of issues and TODOs in this task, will open them up on GitHub shortly!
Feel free to drop by any comments/queries/suggestions, would be glad to get the conversation going.

On a parallel note, how this exactly differs from thefuck, a popular project on GitHub which seems to do the same:
thefuck is a "magnificent app", which is totally rule based and quite robust, I believe. It handles cases like if your git push fails, it will set an upstream and then do git push which is an additional layer of intelligence added. But it's an overkill for my specific use, it needs the rules to be explicitly coded up, secondly it takes decent amount of time evaluating the right matches and then generating the command. Duh isn't doing anything distantly resembling this, it's just a 300 lines of code not dependent on any third party libs, that gives me the closest match from a trie. The distinguishing feature is it takes into consideration the user behaviour on the terminal. If I use a set of 10-15 commands for a powershell session, I am likely to mess up one of those commands, now since I have a corresponding trie for that I can get the right one faster without coding any explicit rule to the repo. Thus the number of commands you can check is not constrained.



See yu in the nex\t postt soooon!
Duh

See you in the next post soon! :)

Till then,
Happy hacking
Adieu


Monday, 4 January 2016

Automate your facebook chats [JUST FOR FUN]

Hola folks!
 We all are on Facebook, yeah, and many times it happens you just don't wish to continue a conversation, reasons can be any and many. Say, you are not in a mood to talk, or though its a good friend who pinged, you are engrossed in some work in the parallel tabs and fb was just left open and hidden amongst the enormity of other tabs or may be its just another unwanted "hii .." (or maybe you are simply excited to automate the chat messages rather than responding in person).

Here is a way to deal with all those in a humane and polite way :)



Preferred browser: Chrome

Do this :
1. In the chatbox settings, click on "See Full Conversation"
2. Press F12 and write the code in console, make sure to make a necessary change before hitting enter, that is,  changing your reply button ID, since for every pageload on FB, this seemingly changes. Usually it is u_0_x though, but do inspect element on Reply Button and copy the id of the button and properly put it in the code.
3. Press Enter and let the fun begin.

var dialogs  = ["hey! ssup?", "oh okay", "nothing much at my end", "btw check out this standup comedy by Biswa https://www.youtube.com/watch?v=wkLmfIxi3No " , "kk", "ahaan", "dude, wait, I will brb", "gotta go, catch you later, bbye :) "];
    var chatBox  = document.getElementsByName("message_body")[0];  
    var replyBut = document.getElementById("u_jsonp_3_6");   //change this id , this may not match
    var diaNum=0;

    var chitInt   = setInterval(function() {
        chatBox.value = dialogs[diaNum] ;
     
        setTimeout(function(){
            replyBut.click();
        }, 4000);
        diaNum=diaNum+1;
      if (diaNum == 8) {
    clearInterval(chitInt);
  }
    }, 60000);


Feel free to play around with the code on my github repo here
Essentially this code is trying to put the dialogs in the array one by one in the chat box. So it starts with "hey!! ssup?" and end by "gotta go, catch you later, bbye :) ". Change these dialogs according to your chatting style to make it more realistic. Close the webpage to stop the script in between.
Also, for now it is one dialog every minute, change depending on the typing speed and availability of your friend (before starting the script).
Don't make it too less otherwise FB will pop up security check to assure its not a robot.

Personally, I got some really unexpected replies on these chats, like "Is this Aditi?" or "what's wrong with your texting language be? ". I needed to type in at times but it was fun overall.
Warning: People will leave frustrated or will be annoyed at times , so , sometimes you need to carry on a real chat just to make them normal and happy again.

I thank to all my friends, for tolerating the irrelevant messages while I was testing the script. Though some of them still don't know it was a script that conversed with them ( O yea B| ) .I am sorry.


PS: 1. Try at your own risk. In case FB blocks you or something crashes, its your responsibility.
2. Its meant for purely educational purposes :D

See you soon in the next post!

Happy Hacking!

Adieu.

A secret love message ~

Hmm, so the trick of putting up a catchy seasonal title worked out, we got you here! Folks, we will be talking about a really cool tech...