Editorial Workflows

App Store Search

public workflow

Install Workflow...

This workflow contains at least one Python script. Only use it if you trust the person who shared this with you, and if you know exactly what it does.

I understand, install the workflow!

This is a workflow for Editorial, a Markdown and plain text editor for iOS. To download it, you need to view this page on a device that has the app installed.

Description: Custom UI to search the App Store for iPhone, iPad and Mac apps. Supports affiliate links. If no text is selected, will set the raw link, else it sets a Markdown link (title and everything).

1.0

Shared by: Phillip Gruneich

Comments: Comment Feed (RSS)

There are no comments yet.

+ Add Comment

Workflow Preview
Run Python Script ?
Source Code
#coding: utf-8
import ui
import urllib
import requests
import json
import editor
import workflow

class searchDelegate():
	def __init__(self):
		self.device_dict = {0: 'software', 1: 'iPadSoftware', 2: 'macSoftware'}
		self.affiliate = '10l4KL' # This is my affiliate code. Set yours here.
	def textfield_should_begin_editing(self,textfield):
		return True
	def textfield_should_return(self,textfield):
		self.query = textfield.text
		self.entity = self.device_dict[textfield.superview.entity_view.selected_index] #The device you selected
		self.country_code = 'us'
		textfield.superview.loading_view.start()
		self.itunes_search(textfield)
	@ui.in_background
	def itunes_search(self, textfield):
		url = 'https://itunes.apple.com/search?term={0}&country={1}&media=software&entity={2}'.format(urllib.quote(self.query),self.country_code, self.entity)
		json_string = requests.get(url).text
		json_dict = json.loads(json_string)
		results = json_dict['results']
		if len(results) > 0:
			output = [app['trackName'] for app in results]
			dict_output = {app['trackName']:'{0}&at={1}'.format(app['trackViewUrl'],self.affiliate) for app in results}
		textfield.superview.loading_view.stop()
		textfield.end_editing()
		table = textfield.superview.table_view
		table.content_size = (table.width, table.height) #rebuilds the size for the content
		table.data_source.items = output
		textfield.superview.list_dict = dict_output
		
class selectApp():
	def tableview_did_select(self,tableview,section,row):
		the_app = tableview.data_source.items[row]
		the_url = tableview.superview.list_dict[tableview.data_source.items[row]]
		sx,sy = editor.get_selection()
		if sx == sy:
			editor.replace_text(sx,sy,the_url)
		else:
			editor.replace_text(sx,sy,'[{0}]({1} "{2} on the App Store on iTunes")'.format(editor.get_selected_text(), the_url, the_app))
		tableview.superview.close()
		workflow.stop()
		
		
class itunesView(ui.View):
	def __init__(self):
		self.bounds = (0,0,320,414)
		self.background_color = '#F6F6F6'
		self.name = 'App Store Search'
		self.loading_view = ui.ActivityIndicator()
		self.loading_view.center = (self.width / 2, self.height / 2)
		self.loading_view.style = ui.ACTIVITY_INDICATOR_STYLE_GRAY
		self.add_subview(self.loading_view)
		search_view = ui.TextField()
		search_view.placeholder = 'Search App Store'
		search_view.text = editor.get_selected_text()
		search_view.frame = (20,20,self.width-40,32)
		search_view.delegate = searchDelegate()
		self.add_subview(search_view)
		search_view.begin_editing()
		self.entity_view = ui.SegmentedControl()
		self.entity_view.frame = (20,72,self.width-40,32)
		self.entity_view.segments = ['iPhone', 'iPad', 'Mac']
		self.entity_view.selected_index = 0
		self.add_subview(self.entity_view)
		self.list_dict = []
		self.list_source = ui.ListDataSource(items=[])
		self.list_source.move_enabled = False
		self.list_source.delete_enabled = False
		self.table_view = ui.TableView()
		self.table_view.row_height = 44
		self.table_view.delegate = selectApp()
		self.table_view.frame = (5,124,self.width, self.height - 124)
		self.table_view.data_source = self.list_source
		self.add_subview(self.table_view)
		
sheet = itunesView()
sheet.present('popover')