Skip to content

Instantly share code, notes, and snippets.

@halspg
Created April 3, 2015 03:26
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 halspg/0f3415ac2226520fb4e2 to your computer and use it in GitHub Desktop.
Save halspg/0f3415ac2226520fb4e2 to your computer and use it in GitHub Desktop.
iTunesプレビューからアイコン画像を取得するサンプル
# -*- coding: utf-8 -*-
import sys
import console
import requests
import ui
import bs4
from PIL import Image
from StringIO import StringIO
import photos
sys.setdefaultencoding('utf-8')
term = console.input_alert('Search Term')
params = {
'term': term,
'country': 'JP',
'media': 'software',
'entity': 'software',
'limit': '10'
}
j = requests.get('http://itunes.apple.com/search', params=params)
feeds = []
if j.status_code == 200:
for item in j.json()['results']:
title = item['trackName']
url = item['trackViewUrl']
price = item['formattedPrice']
feeds.append({
'title': title,
'url': url,
'price': price
})
class iTunesList(object):
def __init__(self, feeds=[]):
self.feeds = feeds
def tableview_did_select(self, tableview, section, row):
url = self.feeds[row]['url']
headers = {'User-Agent': "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10) AppleWebKit/538.34.48 (KHTML, like Gecko) Version/8.0 Safari/538.35.8"}
r = requests.get(url, headers=headers)
soup = bs4.BeautifulSoup(r.text)
icon_url = soup.find('div', attrs={'class': 'lockup product application'}).find('meta').get('content')
icon = requests.get(icon_url, headers=headers)
img = Image.open(StringIO(icon.content))
photos.save_image(img)
tableview.close()
def tableview_number_of_sections(self, tableview):
return 1
def tableview_number_of_rows(self, tableview, section):
return len(self.feeds)
def tableview_cell_for_row(self, tableview, section, row):
cell = ui.TableViewCell()
cell.text_label.text = '{} - {}'.format(
self.feeds[row]['title'],
self.feeds[row]['price']
)
return cell
iTunesList = iTunesList(feeds)
table_view = ui.TableView()
table_view.name = term
table_view.data_source = iTunesList
table_view.delegate = iTunesList
table_view.present('full_screen')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment