Crawling images from a webpage

First extract the url of images
craw.py

1
2
3
4
5
6
7
8
9
10
import urllib2, re
req = urllib2.Request('http://yourwebsite.com/path/to/webpage')
website = urllib2.urlopen(req)

html = website.read()

# Read all png files
imgs = re.findall('"((http)s?://.*?.png)"', text)
for i in imgs:
print i[0]

Read More

Django - Redirect URL to facebook oAuth

In your views.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
from django.http import HttpResponse
from projectname import settings
from urllib import urlencode

def index(request):
params = {
'client_id': settings.FACEBOOK_APP_ID,
'redirect_uri': 'https://www.facebook.com/yourappname/app_' + settings.FACEBOOK_APP_ID,
'scope': 'email,user_birthday,offline_access,publish_stream',
}
redirect_url = 'https://www.facebook.com/dialog/oauth/?' + urlencode(params)
redirect_code = """
<script type="text/javascript">
top.location.href = '%s';
</script>
""" % redirect_url;

return HttpResponse(redirect_code, mimetype='text/html')

Read More

Python: Make a GET request to get JSON data

Make a HTTP request to get JSON content from website.

Create a file json_helper.py

1
2
3
4
5
6
7
8
9
import urllib2
import json

def get_json_by_user_id(user_id):
header = {'Content-Type': 'application/json',}
req = urllib2.Request('http://www.example.com/getmyjson?user_id=' + user_id)
website = urllib2.urlopen(req)

return json.loads(website.read())

Read More

Create Facebook page app using Django with Nginx

This is continue from previous post.

Enable SSL

As we know that Facebook app required SSL, so first we must self-sign a certificate

Read More

Setup Django/mysql in Ubuntu server in VMWare Fusion

Basically the goal is To create a web application that run on VMWare using Python/Django with MySql, and able to accessed by the host OS.

Read More

iOS - Create UITableView with custom cell programmatically

Personally, I don’t like to use Xcode’s interface builder. I felt that everything programmatically will be easier to debug.

This post is share about create table view and custom table cell.

Read More

NSInvalidArgumentException caused by UITableViewCell

I keep hitting an error like this

1
2
3
4
5
2013-03-02 18:34:52.488 StoreSearch[84541:c07] -[UITableViewCell nameLabel]: unrecognized selector sent to instance 0x6a7d880
2013-03-02 18:34:52.489 StoreSearch[84541:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableViewCell nameLabel]: unrecognized selector sent to instance 0x6a7d880'
*** First throw call stack:
(0x14ab052 0xeabd0a 0x14acced 0x1411f00 0x1411ce2 0x3717 0xb4e0f 0xb5589 0xa0dfd 0xaf851 0x5a322 0x14ace72 0x1d6e92d 0x1d78827 0x1cfefa7 0x1d00ea6 0x1d00580 0x147f9ce 0x1416670 0x13e24f6 0x13e1db4 0x13e1ccb 0x1394879 0x139493e 0x1ba9b 0x20bd 0x1fe5)
terminate called throwing an exception(lldb)

Read More

Restrict Git commit message to certain pattern

For certain reason, some time you want your commit message to follow a standard pattern.

i.e. PJK-001: my message

Want to have a project code and assignment code there.

Read More

SQL - SELECT SUM joined with another table

I had always face a scenerio like this:

There are 2 tables

1
2
user(id, first_name, last_name)
transaction(id, qty, user_id)

Read More

Vim - Set indentation based on file type

Let say now you want your .js (JavaScript) file to 2 space indent.

Create a file named javascript.vim (NOT js.vim)

Read More