Monday, 17 March 2014

Python's beautifulsoup is beautiful

If you're looking for a way to scrape data from a html - python is the way.
"Beautiful Soup is a Python library designed for quick turnaround projects like screen-scraping."

I've found myself in a situation where I needed to download a lots of different images. Since I was too lazy to do it manually I found beautifulsoup to be very helpful. In my case i needed to download only images with certain alt and src description. I would use that src then to access files on site/y and save them to a local folder.

If you are using linux installing beuatifulsoup should be a straightforward operation. However, if you are on windows I recommend installing cygwin. You can use your setup.exe any time to install new packages.

For more info on soup and how to install it go here.


import urllib
import re
import errno
import os, os.path
from BeautifulSoup import BeautifulSoup


url ="http://site.com/x"

file  = urllib.urlopen(url)

soup = BeautifulSoup(file.read())


j = len(soup('img', {'alt': re.compile("Flag of") }  ))

nameList = [None]*j
tagList = [None]*j


for i in range (0,j):
   flag_alt = soup('img')[i]['alt'].encode('ascii', 'ignore')
   flag_src = soup('img')[i]['src'].encode('ascii', 'ignore')
   nameList[i] = flag_alt.split("of ")[1]; 
   tagList[i] = flag_src.split("/")[-1]; 
  
for i in range (0,j): 
   imgName = nameList[i].lower().replace(" ","_")+".png"
   filename = os.path.join(os.path.dirname(os.path.abspath(__file__)), imgName)
   if not os.path.exists(filename):
       urllib.urlretrieve("http://site/y"+tagList[i], imgName)

num_of_files = len([name for name in os.listdir('.') if os.path.isfile(name)])  
if j== num_of_files -1:
   print "All the files downloaded: "+str(j)
else:
   print str(j-num_of_files-1)+" not dowloaded!"

Sunday, 16 March 2014

Using drawables, yay (Part 1)

So, let's say you need to make an image gallery or you need to use a large number of images in your app, but you don't want to import all of those images by hand. What do you do ?

All your drawable data should be located in your_project/res/drawable folder. In most cases this folder is not auto-generated in eclipse, so you would need to create it by hand. Image resources outside of this folder will not be accessible. Keep in mind names of the resources in drawable should be lowercase, also contain only letters a-z, numbers and an underscore. Not doing this will cause an error in your XML files and that could probably omit the R.java file from being generated !





So we have our drawable folder all set. Now, how do make an image appear magically in our app ?

Each element in our app has to be in a view. Each view has a parent view and can also have children views.
A system of this views is called View Hierarchy. You can open activity_main.xml to have a glimpse at your xml defined UI. There is more than one way of defining are views in view hierarchy which we will use later.

LinearLayout children mostly behave like bricks. You can stack them vertically or horizontally.
Here is an example:

           <LinearLayout
         android:id="@+id/LinearLayout2"
         android:layout_width="match_parent"
         android:layout_height="140dp"
         android:orientation="horizontal" 
         android:gravity = "center"
         > 
     </LinearLayout>

We gave our view a unique name with "@+id/LinearLayout2" which we will reference later. It has a match_parent attribute which basically says: "I can be as big as my parent". However we've set our height to be statical. Orientation must be supplied when LinearLayout is used. 
android:gravity tells the child view to place itself in the center of both x and y-axis.

Let's first show how to set up a single image and later we'll load them up dynamically.
Remember the "@+id/LinearLayout2" ? Now we will use it to get our view.

MainActivity.java (inside the onCreate() )


  
 LinearLayout2 = (LinearLayout) findViewById(R.id.LinearLayout2); // findViewById returns an int !
 image = new ImageView(MainActivity.this); // creates a new ImageView
 setImage(); // this is a method

Here we declare a new LinearLayout to which we will append the image. We need to cast the value returned by findViewById, because it returns an int, but we need it to return a LinearLayout.

Here is the method i was talking about. We need to make it public and void, because we don't need to return a value. Also, don't forget to make the variables LinearLayout2 and image public.



        public void setImage(){
  LinearLayout2.removeView(image); 
  image.setImageResource(R.id.new_image); 
  LinearLayout2.addView(image); 
 }

First we need to make sure that there were no views prior added to LinearLayout, so we use removeView(). Then we setImageResource() for the ImageView using R.id.new_image, where new_image is an image resource in the drawable dir. Finally we add it to the LinearLayout2.

In the second part i will show how to set images dynamically using Fields.




Let's make an android app !

I've been looking for a simple project that could help me consume my free time and in the same time be useful. So I decided to give android a go!

I have to admit I had tried programming for android before, but this time I didn't bother myself with time consuming and exhausting tutorials out there. I went straight through the door - make a goal, get a hang of things, if it doesn't work, use Google or ask around...make simpler examples in the programming languages you know, and if it doesn't work still...there is more than one way to go.

 I ask you how many exits out of the room you see ? You say: One, the door... ? I say, 2 windows, a ventilation shaft, the walls can be broken down...If it seems like you've just hit a brick wall with your head, keep in mind that  you're probably facing the wrong way...but what ever you do..DON'T give up. I've gave up on learning many things. And unfortunately I regret every time.  

I'm going to post all of my code here, as well as any bugs or problems/and how i fixed them. 
I've been busy building a simple android quiz-like game of flags. The object of the game is to guess the name of each flag. Would you guess that. As you can see it looks rather simple, but it works !