Forum Archive

Progress Tracker on App Badge

Webmaster4o

This is a quick utility I put together with about an hour. Thanks to @filippocld for the app badge discovery. It's a basic system by which the progress of one or more tasks can be displayed as a badge on the app icon. Overall progress of tasks can be displayed either as percent complete or as estimated time remaining.

After each task is complete, a notification is sent. It uses objc_util, so requires 2.0. The gist is here. If anyone complains, I'll move it to a repo.

Webmaster4o

As far as an example, this script downloads a YouTube video while showing progress as an app badge:
```
import requests,bs4,urllib2
import ProgressBadge as pb

pb.setDisplayMode('time remaining')

Gets the raw video url for the video using the keepvid service.

vidurl='https://www.youtube.com/watch?v=fzievdlaVIU'
url='http://www.keepvid.com/?url={}'.format(vidurl)
soup = bs4.BeautifulSoup(urllib2.urlopen(url).read())
link=[l.get('href') for l in soup.select('a') if l.get('href') and 'googlevideo' in l.get('href')][0]

Holds the progress bar

try:
with open('vid.mp4', "wb") as f:
response = requests.get(link, stream=True)
#Total length of the video
total_length = response.headers.get('content-length')
#The length of the video is the "top value" for our task
task=pb.Progress(int(total_length))
#Update the task as we download and write the video
for data in response.iter_content():
f.write(data)
task.increment(len(data))
except:
#Clean up, which means take away the badge from the app.
pb.stop()
raise

clean up

pb.stop()

Olaf

Looks quite cool, @Webmaster4o!

ccc

The gist is here. If anyone complains, I'll move it to a repo.

Of course, I would be the only one who would ever make such a complaint . ;-) @Webmaster4o This is super cool! The addition of the objc_util module opens lots of doors and it is exciting to see what you folks are doing with the new superpowers that @omz has given to us.

JonB

Now: get background transfers that work when the app is stopped working:)

Webmaster4o

UPDATE: I restructured the code. Since you never need to have more than one container, now the utility handles that automatically. You can have multiple tasks running, and they will automagically be combined on the app badge. Also, now there's an option with

pb.setDisplayMode('time')

to have the badge display an estimated time remaining. The gist has been updated to reflect this, as has the example posted previously.

disorientedp

Wow, this is excellent.

disorientedp

I need to start playing around with objc_util

ccc

Comments on the gist... ;-)

Webmaster4o

@ccc Not entirely sure what that does, I'm assuming it replaces all my ifs and elses, but I don't use format too often. I'll look at the docs.