Keyboard automation

Problem

I had a text on the clipboard and I wanted to automate the Shift+Insert (paste) keypress. That is, I wanted to paste the content of the clipboard using a script.

Solution

I have a Bash script and when I run this script, this script at the end should paste the content of the clipboard.

First I came up with a Python solution using the excellent PyAutoGUI library:

python -c "import pyautogui; pyautogui.hotkey('shift', 'insert')"

It did the job but it required the pyautogui library to be installed. Then I found a solution that doesn’t need this dependency. This one relies on the Linux command xdotool:

xdotool key Shift+Insert

That’s it. Done :)

[ C lang] reading a text file line by line

Problem
I had a text file that I was reading line by line in C. Every line was tokenized. The file looked like this:

1978	Aachen Cathedral	DE	C	EUR	0	3
1978	City of Quito	EC	C	LAC	70	2
1978	Galápagos Islands	EC	N	LAC	14066514	1
...

I converted every year to an integer. From the 2nd line I got 1978 (as int), but in the first line the string to int conversion failed.

Solution
It turned out that the token “1978” in the first line was 7 characters long, not 4. At that point I opened the file in a hex editor and there were 3 special bytes at the beginning: EF BB BF. This is a byte order mark (BOM).

bom

As a quick fix, I removed those 3 bytes in the hex editor.

I tried it under Python too and it caused a problem there too:

>>> f = open("input.txt")
>>> s = f.readline()
>>> s
'\ufeff1978\tAachen Cathedral\tDE\tC\tEUR\t0\t3\n'
>>> s.split()
['\ufeff1978', 'Aachen', 'Cathedral', 'DE', 'C', 'EUR', '0', '3']
>>> s.split()[0]
'\ufeff1978'
>>> int(s.split()[0])
Traceback (most recent call last):
  File "", line 1, in
ValueError: invalid literal for int() with base 10: '\ufeff1978'
>>>

If you know how to ignore those bytes during file read, please leave a comment. I’m interested in both C and Python solutions.

Getting started on Twitter

Problem
You have a site and you want to create a Twitter site for it too where you want to send posts, probably in an automatic way.

Solution
If you have a gmail account, you can use it to create several Twitter accounts. The trick is to make the e-mail addresses different with a dot. Example: myself@gmail.com and my.self@gmail.com . If you send an e-mail to these addresses, they’ll arrive in the same mailbox, though Twitter treats them as two different addresses.

Then, follow the steps at https://python-twitter.readthedocs.io/en/latest/getting_started.html to register your app.

Sample Python code
Here is a simple example to post a tweet:

#!/usr/bin/env python3

import config as cfg
import tweepy

def get_api(cfg):
    d = cfg.twitter_keys
    auth = tweepy.OAuthHandler(d['consumer_key'], d['consumer_secret'])
    auth.set_access_token(d['access_token'], d['access_token_secret'])
    return tweepy.API(auth)

def main():
    api = get_api(cfg)
    tweet = "just a test"
    status = api.update_status(status=tweet)    # update_status -> send a tweet

if __name__ == "__main__":
    main()