Tuesday, January 5, 2016

Ribbon Wordpress Theme - Arabic Version

In case you don't know about Ribbon, It is a free responsive WordPress magazine theme.

A few years ago, I used it for a website/blog which I used to use and maintain. To reach an appealing view, it was not enough to change direction to RTL or use an automated tool that used to exist back then. So I spent some time to translate some segments and style a few elements. The final state was as follows:

Ribbon is a great Wordpress theme, and I really liked its performance and clean look. So, here it is in Arabic. I hope it would be useful for someone out there.

Download Arabic Ribbon


Friday, December 18, 2015

Download RSS Media Files using Python

Today I wanted to download a course from an RSS feed. The media files (mp3) were attached to the RSS entries. So instead of downloading tens of files one by one, I wrote a script!

With the help of Alvin's post to parse RSS with Python, I attached a download method and the script was ready to do the work for me.

First, here is the script:
[rss_downloader.py]
#!/usr/bin/python

import feedparser
import sys
import urllib2

#
# Takes a url and a directory for saving the file. Directory must exist.
#
def download(url, dir_name):
    file_name = url.split('/')[-1]
    u = urllib2.urlopen(url)
    f = open(dir_name+'/'+file_name, 'wb')
    meta = u.info()
    file_size = int(meta.getheaders("Content-Length")[0])
    print "Downloading File: %s (Size: %s Bytes)" % (file_name, file_size)

    file_size_dl = 0
    block_sz = 8192
    while True:
        buffer = u.read(block_sz)
        if not buffer:
            break

        file_size_dl += len(buffer)
        f.write(buffer)
        status = r"%10d  [%3.2f%%]" % (file_size_dl, file_size_dl * 100. / file_size)
        status = status + chr(8)*(len(status)+1)
        print status,

    f.close()

#
# Take url and directory parameters from user call
#
url = sys.argv[1]
dir_name = sys.argv[2]

#
# Get the feed data from the url
#
feed = feedparser.parse(url)

#
# Collect urls to download
#
urls_to_download = []
for entry in feed.entries:
    links = entry.links
    for link in links:
        if link.type == u'audio/mpeg':
            urls_to_download.append(link.href)

print("Files count: %s" % (len(urls_to_download)))

#
# Download files
#
for url in urls_to_download:
    download(url, dir_name)
    # print(url)


You just call the script, pass RSS url, and the directory to save the files.
python rss_downloader.py http://rss.dw.com/xml/DKpodcast_dwn1_en /home/madly/DeutschWarumNicht/serie1

This will parse the RSS feed, print the available links with type "audio/mpeg" (you can change this to be a value passed by the user), and download them with a progress display
This it. But there is more...

After doing this, I realized that I could simply print the output, copy the links all together to my download manager as a batch download, and enjoy the features of my download manager. After all, I wanted to download the files, not make a full application. Dummy me :D

However you choose to go with the script, I hope you find it useful.

Monday, November 30, 2015

CSE 321 Project - Part 2 - Install Ruby, Rails, PostgreSQL, MySQL and others

This is one post in a series of posts related to CSE 321 - Software Engineering project for ASU CSE 2017 students. The series starts from setting up the environment, to planning the work on the website project, to implementation

After Installing Lubuntu/Ubuntu in a VM in part 1, we want to prepare it for development.

Thinking about the required project, this is what I want to install.
  • Ruby
  • Rails
  • PostgresSQL (or MySQL)
  • Node.js
  • Git
  • Gitg (a graphical tool to use with Git)
  • Atom editor (or you can install Sublime if you like)
Plus enabling copy & paste between the VM and the computer, and enabling flexible display resolution to suit your taste. And I will start with this.

Install Guest Additions

Now we need to install some additional packages to add more power to our VM. This will make our job easier in scaling the VM window (and keeping the aspect ratio), and enables copy&paste between the VM and the host computer to easily follow the next instructions.

If you have a toolbar in the outer VM window, go to Devices -> Insert Guest Additions CD image.... If you cannot find the toolbar, press Right Ctrl + Home buttons and the menu should appear. (if you still fail, press Right Ctrl + C and search for the toolbar again)

Once you do this, a virtual CD will be mounted (inserted) into the machine to install some additional packages.

In case of Ubuntu, the process should start immediately once you click Run in the box that will appear.

In case of Lubuntu, you will have something like this:

Click OK. Then run autorun.sh in the window that will appear (this is the content of the virtual CD).

Then choose Execute.

Authorize the script to have additional privileges by entering your password and pressing OK.

The script will run for a few seconds, installing some packages. Once finished you will have to press Enter to exit.

Extra: In case of Lubuntu, you need to install another package. Open the terminal by pressing Ctrl + Alt + T or clicking the start menu -> System Tools -> XTerm. Then type (you cannot copy at this point):
sudo apt-get install --yes virtualbox-guest-dkms
Then enter your password when it asks for it (it will download about 60MB).


Now shutdown the machine, go to this VM Settings -> General -> Advanced tab, and set Shared Clipboard to Bidirectional. This way you can copy instructions from your host OS to the VM terminal, and copy any error message you face from the VM terminal to -for example- your browser in the host OS.

Now let's start the VM again.

This time, try to copy any text (for example, a url) from your host OS. Then search for Firefox browser in the VM and paste it. It works!

One more thing has happened. If you resize the VM window, it will scale and keeps the aspect ratio. If you find the screen is getting 'compressed' and losing the correct ratio, press Right Ctrl + C.

Now we have a clean and helpful environment to work with.
Let's start with the development installations. Open the terminal and get ready!

Install Ruby

(references used: DigitalOcean, GoRails)

First there are some packages to install before Ruby.

Update out package list
sudo apt-get update

Install git, curl, and some other packages for compilation (~90MB)
sudo apt-get install git-core curl zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev python-software-properties libffi-dev

Now install rbenv (the tool for manging Ruby versions, better than installing Ruby directly)
cd
git clone git://github.com/sstephenson/rbenv.git .rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
git clone git://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc

Next, install Ruby and set the installed version to be the default. (this will take a while to download and compile)
rbenv install -v 2.2.3
rbenv global 2.2.3

Now if you type
ruby -v
you will see the default ruby version.

Let's add one more step, disable documentation installation. We do not need them.
echo "gem: --no-document" > ~/.gemrc

Install Bundler Gem

This is the first gem to install. It is responsible for handling later gems in Rails projects.
gem install bundler

Install Rails
gem install rails -v 4.2.5

When it finishes, if you type
rails -v
you will see the default rails version.

Install Node.js

(reference: https://github.com/nodesource/distributions)
Node.js is a platform for development using Javascript. Rails uses Node.js in a certain step when joining multiple Javascript files together for better performance. You may not need this at first, but let's setup the environment the right way for future use and future reference.

curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash -
sudo apt-get install -y nodejs

Now type
node -v
to check the installed version.

Install PostgreSQL

PostgreSQL is the biggest competitor to MySQL database. It is getting more popular, so it is nice to stay up-to-date and give it a try. But don't be scared. at first, it will not matter if you use MySQL or PostgreSQL because most of your interactions will be through Rails. (You can skip it and install MySQL)
sudo sh -c "echo 'deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdg main' > /etc/apt/sources.list.d/pgdg.list"
wget --quiet -O - http://apt.postgresql.org/pub/repos/apt/ACCC4CF8.asc | sudo apt-key add -
sudo apt-get update
sudo apt-get install -y postgresql-common
sudo apt-get install -y postgresql-9.4 libpq-dev

Create PostgreSQL User

The last step is to create a new db user for later.
sudo -u postgres createuser cse -s
If you would like to set a password for the user, you can do the following
sudo -u postgres psql
postgres=# \password cse 
then enter the password you like.

Install MySQL

Use this command to install MySQL server and client.
sudo apt-get install mysql-server mysql-client libmysqlclient-dev
When prompted, you can choose to enter a password for the root user of MySQL or leave it blank. Whatever you choose, then use the down arrow to go to OK line and hit enter.


The root user has access to all databases. It is not safe to use it in production environment. It is better to create a new user for each project and give access to project's database only. But since this is a development environment, we can use root user.

Install Atom Editor

Now we need a nice editor to help us with developmen. Recently, I switched from Sublime Text to Atom Editor. So I will continue with using Atom. Feel free to use whatever you want.
You can install Atom by downloading the installer (*.deb) from their home page, or install it from the terminal. To avoid explaining how to share folders between VM and host, I will install it from the terminal. You can follow my steps or download the installer from the VM browser.
sudo add-apt-repository ppa:webupd8team/atom
sudo apt-get update
sudo apt-get install atom

Install Gitg


The last optional step is to install Gitg. It is a graphical interface to use Git. It make things a bit easier when it comes to committing changes, reviewing history, and other operations you may need.

sudo apt-get install gitg


That's all folks! Your machine is ready. You can compress the machine and share it with others or keep it as a backup as a ready-to-use machine.