Tools

- Best tool to back up your tumblr blog incrementally: https://github.com/bbolli/tumblr-utils/blob/master/tumblr_backup.md
	- It even stores the json coming from the tumblr API.
- Disk usage utility, an alternative to `df`: https://github.com/muesli/duf

Coding stuff

  • mongodb insert if not exists: use update_one with upsert=True
    • This inserts if the record doesn’t exist,
    • Updates the one already in the db if exists.
import pymongo
j = {id: 'something'}
db.items.update_one(
{'id': j['id']},
{'$set': j},
upsert=True)

If you see this error: TypeError: upsert must be True or False or PyMongo upsert throws “upsert must be an instance of bool” error it’s probably due to the fact that you are trying upsert with {'upsert': True}, the correct syntax for pymongo library is like this upsert=True as keyword parameter rather than a dictionary unlike mongodb cli.

  • mongodb find the most frequently occurring tags in your collections where tags is an arrayfield in the collection.

Imagine the collection is something like this:

[
	{
		"_id": 123,
		"title": "foo",
		"tags": ["yolo", "hashtag", "something"]
	},
	{
		"_id": 124,
		"title": "bar",
		"tags": ["yolo", "otherhashtag"]
	},
]

To get something like this: {'yolo': 2, 'hashtag': 1, 'something': 1, 'otherhashtag': 1}, you need to aggregate (beware it takes some time):

db.my_collection.aggregate([
  { "$unwind" : "$tags" },
  { "$group": { "_id": "$tags", "count": { "$sum": 1} } },
  {
    "$group": {
      "_id": null,
      "counts": {
        "$push": {
          "k": "$_id",
            "v": "$count"
        }
      }
    }
  },
  {
    "$replaceRoot": {
         "newRoot": { "$arrayToObject": "$counts" }
    }
  },
])

Moreover you probably will want this to be in a json file for further analysis. So you can redirect the output of Mongo CLI like this:

mongo "MONGO_CONNECTION_STRING" \
	--quiet --eval \
	db.my_collection.aggregate([ \
		  { "$unwind" : "$tags" }, \
		  { "$group": { "_id": "$tags", "count": { "$sum": 1} } }, \
		  { \
			"$group": { \
			  "_id": null, \
			  "counts": { \
				"$push": { \
				  "k": "$_id", \
					"v": "$count" \
				} \
			  } \
			} \
		  }, \
		  { \
			"$replaceRoot": { \
				 "newRoot": { "$arrayToObject": "$counts" } \
			} \
		  }, \
		]).pretty()' > tag_frequency.json
  • Email sending problems from VPS through an SMTP server (eg. mailgun)
    • I have written a medium size blog post around how to tackle this problem at https://hakanu.net/production/2020/10/11/smtp-timeout-error-from-vps/
  • To change your Chrome’s default new tab page, you can use this extension: https://chrome.google.com/webstore/detail/new-tab-redirect/icpgjfneehieebagbmdbhnlpiopdcmna
    • I have prepped a small HTML with bulma.css to list my projects and frequently used self hosted server addresses so that I don’t need to keep everything in mind. YOu can even give direct html file path on the file system. You don’t need to host it.
      • eg. file:///foo/bar/path/index.html
      • PS: I keep all the data in a js file and render in the UI with javascript.
    • Here is my github repo of the homepage I prepped: https://github.com/hakanu/chrome_home
    • There are better projects under /r/selfhosted but I will stick with mine for now.
  • If you keep switching between Mac and PC, you may be as confused as me. I need to keep rewiring my brain while using chrome, terminal and vim. Because CMD and Win and Ctrl buttons are behaving differently. Especially if you use the same keyboard for both of these (which I do).
    • In order to fix this (a little bit better), I tried many methods like switching ctrl and win keys for good by using something called Sharpkeys which kinda ruined everything. I needed to revert back. And it asks for restart every time. I don’t like restarting my pc
    • Anyways, my final attempt is using AutoHotKey (AHK) program which is scriptable window and key management script. I was able rewrire win button to ctrl for Chrome and Windows Terminal to have the analogous behaviour I have in Mac. These two is what I used mostly.
      • cmd + t => New tab
      • ctrl + c => Vim visual mode.
    • Here is my AHK script to macify your windows keyboard behavior:
      • This switches windows button to control and keeps control same.
      • You lose some goodies with windows but still it works like Mac keyboard.
      • I also want to rewire win + shift + s for screenshottaking to ctrl + shift + 4 like in mac (cmd + shift + 4). Leave a comment if you are able to pull this off or you have the same problem.
#IfWinActive Visual Studio Code
LWin::LCtrl
return

#IfWinActive TERMINAL_TITLE
LWin::LCtrl
return

#IfWinActive Google Chrome
LWin::LCtrl
return