Monday, June 11, 2012

Sending XML Queries in python using urllib2


Query is the xml string that you want to send.  Modify the content type according to your needs.

    def send_xml_query(self, query, host, port):



        query = query.replace ('\n', '')

        headers = {"Content-type": "application/xml"}

        host = 'http://%s:%s' % (host, port)

        request = urllib2.Request(host, query, headers)

        response = urllib2.urlopen(request)

        return request, response

Wednesday, April 18, 2012

Search your sourcecode better with ack..!

One of my good friends told me about a new tool that I could use to search for strings in my source code.

It is called ack and it is pretty easy to install and use.

I have a RHEL 5.5 system and it works without a lot of hassle.

Below is the URL for it.

http://betterthangrep.com/install/

The URL for the actual tool is
http://betterthangrep.com/

As the name suggests, this is a better than grep and I second it.

So who is this useful for?

I would say developers who use search more often to look for certain specific strings in their source code would really love this tool.

Advantages:

  • It's fast because it only searches the stuff it makes sense to search.
  • Searches entire trees by default while ignoring Subversion, Git and other VCS directories and other files that aren't your source code.
  • Where grep is a general text search tool, ack is especially for the programmer searching source code. Common tasks take fewer keystrokes.
  • Ack costs nothing. It's 100% free.

Thursday, March 22, 2012

Verify Apple store (In-app purchase) receipts in python

Python is very versatile and dynamic programming language and I use it on a daily basis.

Currently I was working on the In-app purchases from the App store and was wondering how I can verify the app store receipt data on the python prompt.

I found it easy in python to communicate to the app store and verify the app store receipt.  This article talks about how you can do it on the python prompt.  Hopefully this works and helps anybody trying to verify the receipt data from app store through the sandbox/app store.

I was trying to look for examples on the web if somebody had already done it in python but did not find one.  So here it goes.!!!


import urllib, urllib2
import simplejson as json
import datetime
url = 'https://sandbox.itunes.apple.com/verifyReceipt'
receipt_data = 'receipt_data'
password =  'your_password'

data = { "receipt-data": receipt_data, "password": password }
headers = {'Content-Type': 'text/Json; charset=utf-8'}
dataj = json.dumps(data)
request = urllib2.Request(url, dataj, headers)

# In case you are wondering what is being sent and the format
print "request%s" % request.get_data()
# This shows the method either POST or GET (App store wants POST and not GET)
print "request type %s" % request.get_method()
start_time = datetime.datetime.now()
response = urllib2.urlopen(request)
wait_time = datetime.datetime.now() - start_time
print('Wait time for validation call: %s' % wait_time)
response_json = json.loads(response.read())
print response_json




{'status': 21006, 'receipt': {'purchase_date_pst': '2012-03-16 15:33:58 America/Los_Angeles', 'expires_date': '1331937538000', 'product_id': 'your_product_id', 'original_transaction_id': '1222000034404994', 'expires_date_formatted_pst': '2012-03-16 15:38:58 America/Los_Angeles', 'original_purchase_date_pst': '2012-03-16 15:33:59 America/Los_Angeles', 'item_id': 'your_item_id', 'original_purchase_date': '2012-03-16 22:33:59 Etc/GMT', 'expires_date_formatted': '2012-03-16 22:38:58 Etc/GMT', 'bvrs': 'your_version', 'original_purchase_date_ms': '1331937239000', 'hosted_iap_version': '2.718', 'purchase_date': '2012-03-16 22:33:58 Etc/GMT', 'web_order_line_item_id': 'your_id', 'purchase_date_ms': '1331937238000', 'bid': 'your_bid', 'transaction_id': '1222000034419520', 'quantity': '1'}, 'latest_expired_receipt_info': {'purchase_date_pst': '2012-03-17 00:59:55 America/Los_Angeles', 'expires_date': '1331971495000', 'product_id': 'your_product_id', 'original_transaction_id': '1222000034404994', 'expires_date_formatted_pst': '2012-03-17 01:04:55 America/Los_Angeles', 'original_purchase_date_pst': '2012-03-16 15:33:59 America/Los_Angeles', 'item_id': 'your_item_id', 'original_purchase_date': '2012-03-16 22:33:59 Etc/GMT', 'expires_date_formatted': '2012-03-17 08:04:55 Etc/GMT', 'bvrs': 'your_version', 'original_purchase_date_ms': '1331937239000', 'purchase_date': '2012-03-17 07:59:55 Etc/GMT', 'web_order_line_item_id': 'your_item_id', 'purchase_date_ms': '1331971195000', 'bid': 'your_bid', 'transaction_id': '1222000034486923', 'quantity': '1'}}

Below is the URL that has additional details on the error codes and API details.

http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/StoreKitGuide/VerifyingStoreReceipts/VerifyingStoreReceipts.html