Skip to content

Instantly share code, notes, and snippets.

@yulu
Last active October 28, 2015 06:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yulu/8b9e69c66a315bf306e3 to your computer and use it in GitHub Desktop.
Save yulu/8b9e69c66a315bf306e3 to your computer and use it in GitHub Desktop.
catch result
__author__ = 'yulu'
import json
import urllib
from PIL import Image
import urlparse
from libmproxy.protocol.http import decoded
import os
import random
'''
http://api.s.m.taobao.com/search.json?
vm=nw
&search_wap_mall=false
&region=
&ttid=212200@taobao_android_5.2.7
&utd_id=U2MS66ueEOEDAB9gxO+Mjeu8&cat=1
&setSpApp=imgsearch
&searchimg=TB2rr0dLcpXXXXbNXpXXXXXXXXXX_!!1060815481-0-imgsearch.jpg
&pict_property=&m=api4etao&pict_search=1
'''
def save_image(response, imagepath):
decoder = json.JSONDecoder(strict=False)
result = decoder.decode(response)
items = result["itemsArray"]
hash = random.getrandbits(32)
imageFolder = 'image_folder' + str(hash)
if not os.path.exists(imageFolder):
os.mkdir(imageFolder)
# download uploaded image
qurl = 'http://img02.taobaocdn.com/imgextra/i2/1060815481/' + imagepath[0]
print qurl
queryImagePath = os.path.join(imageFolder, '_query.jpg')
urllib.urlretrieve(qurl, queryImagePath)
for item, count in zip(items, range(0, len(items))):
# get the webp image path for good quality image
url = item['pic_path']
surl = url.split('.jpg_')
url = surl[0] + '.jpg_320x320xzq100.jpg_.webp'
# download webp
filename = os.path.join(imageFolder, str(count)+'.webp')
urllib.urlretrieve(url, filename)
# convert the webp image to jpg and save
im = Image.open(filename).convert("RGB")
jpgfilename = os.path.join(imageFolder, str(count)+'.jpg')
im.save(jpgfilename, "JPEG", quality=100)
os.remove(filename)
print 'save: ' + str(count)
# @concurrent # Remove this and see what happens
def response(context, flow):
print flow
if flow.request.host == 'api.s.m.taobao.com':
# get uploaded image path from url query
parsedurl = urlparse.parse_qs('http://' + flow.request.host + flow.request.path)
imagePath = parsedurl['searchimg']
# decode the json response
with decoded(flow.response): # automatically decode gzipped responses.
result = flow.response.content
result = result +"\n\n"
# save_image(result, imagePath)
with open("response.txt", "a") as myfile:
myfile.write(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment