Thursday, May 11, 2017

Come C tricks

Swapping the values of two variables with out using a third!!
x = x ^ y;
y = x ^ y;
x = x ^ y;
All hail the XOR operation
Reference: https://www.cs.umd.edu/class/sum2003/cmsc311/Notes/BitOp/xor.html


int c;
c &= (c-1);
check if c == 0

This allows you to check if C is a power of 2 (i.e. 0,1,2,4,8...)



Reading sentences (including spaces) from stdin:

scanf("%s",str); can't read spaces in input string.
To do that, you have to: scanf(" %[^\n]s",str);

Note the space before % within the quotes.

Reference: https://gpraveenkumar.wordpress.com/2009/06/10/how-to-use-scanf-to-read-string-with-space/

Monday, May 1, 2017

Set Elon's latest tweet as your desktop wallpaper

If you have an Ubuntu box, you can use a script to to set your wallpaper's to Elon's Latest Tweet.

Here is how you setup:
  1. Install required packages.
  2. Download tweet.sh - an OAuth based twitter client implemented completely as a bash script.
  3. Get Token keys for from your Twitter account and save it in the config file. You need this for OAuth that twee.sh uses.
  4. Get a free account on http://www.page2images.com/ and fetch your API keys. This service allows you to generate screenshot of any supplied URL - you can use this service to to fetch PNGs's of Elon's tweets. We do screenshots because aren't just interested in tweet's text but also any images that Elon chooses to attach with his tweets.
  5. In the batch script (elon_tweet.sh) appended below, configure the pages2images API key
  6. Setup the batch script (elon_tweet.sh) as a cron job which runs once every minute.
You can set this script up on a Raspberry Pi running Raspbian connected to your TV as well. Its always nice to have some inspiration trickling in while you work.



Here is how my desktop looks like
Here's how to go about putting the pieces together:
  1. Open a terminal window and install the required software on your ubuntu box:
    sudo apt-get install jq imagemagick pngcrush nkf
  2. Clone https://github.com/piroor/tweet.sh and place tweet.sh and tweet.client.key in your home directory
  3. While logged into Twitter, visit https://apps.twitter.com/ and create a new app and generate the required Token and Keys and place them in tweet.client.key
  4. Mark tweet.sh as executable:
    sudo chmod +x tweet.sh
  5. Register on http://www.page2images.com/ and get your API key
  6. Create a new script called elon_tweet.sh in your home folder and copy and paste the code snippet appended below.
  7. Edit elon_tweet.sh and insert the pages2image API key at the appropriate location.
  8. Mark elon_tweet.sh as executable:
  9. Set the script to run once every minute:
    crontab -e
    and insert the following line:
    * * * * * bash ~/elon_tweet.sh
The script will run once every minute and will check if there is any new tweet from Elon since last check. If there is, it will submit the URL for screenshot generation and exit. When the script is run again at the next consecutive minute, it will fetch the image, crop it and set it up as wallpaper. If Elon has put up a new tweet, the URL of this new tweet is submitted for screenshot and the script exits. In such situation, you will miss out the 2nd tweet if the time difference between 1st and 3rd tweet is less than 2 minutes - this can be fixed in the script, but it seemed to be a bit much. Thank God Elon doesn't go crazy on twitter. All the tweets are saved in the Pictures folder.

Tweet screenshots are saved in Pictures folder
This is what the cropped tweet looks like

This is what the cropped tweet looks like


elon_tweet.sh