-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbrushdownloader.py
executable file
·108 lines (95 loc) · 4.05 KB
/
brushdownloader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env python
# Author: Philipp Tkachev
# Copyright 2013 Philipp Tkachev
# License: GPL v3
# Version 0.1
# GIMP plugin to download brushes from ProGimp.RU
from gimpfu import *
import os, time, urllib2, json
gettext.install("gimpcloud", (gimp.directory + os.sep + 'plug-ins' + os.sep + 'locale'), unicode=True)
def echo(args):
"""Print the arguments on standard output"""
pdb.gimp_progress_set_text(args)
def build_req(url):
req = urllib2.Request(url)
req.add_header('Referer', 'http://www.progimp.ru/')
req.add_header('User-agent', ("Gimp/%d.%d.%d" % gimp.version))
return req
def download_tags():
tags = ["", "Small", "Middle-size", "Big size"]
tags_n = (("", "Small"), ("Middle-size", "Big size"))
return tags_n
def download_brushes(path, size):
page = 1
total_pages = 1
p = 0.0
while page <= total_pages:
rpc_url = "http://www.progimp.ru/rpc.php?p=get.brushes&filter=%s&pg=%s" % (size, page)
gimp.progress_init(_("Looking up for new brushes..."))
rpc_req = build_req(rpc_url)
pg_url = urllib2.urlopen(rpc_req)
if pg_url:
res = pg_url.read()
br_list = json.loads(res)
if br_list[u'pages']:
total_pages = br_list[u'pages']
if br_list[u'brushes']:
br_list_len = float(br_list[u'brushes_total'])
for cbr in br_list[u'brushes']:
p = p + 1.0
pdb.gimp_progress_update(p / br_list_len)
echo(_("Getting... ") + cbr[u'name'])
brush_file_path = path + os.sep + cbr[u'file']
if os.path.isfile(brush_file_path):
echo(cbr[u'name'] + _(" already exists!"))
else:
get_brush_url = "http://www.progimp.ru/downloads/brushes/%s/get/" % cbr[u'id']
fd = open(brush_file_path, "wb")
if fd:
get_brush_req = build_req(get_brush_url)
get_brush_resp = urllib2.urlopen(get_brush_req)
total_size = get_brush_resp.info().getheader('Content-Length').strip()
total_size = int(total_size)
bytes_so_far = 0
chunk_size = 4096
while 1:
chunk = get_brush_resp.read(chunk_size)
if not chunk:
break
else:
fd.write(chunk)
bytes_so_far += len(chunk)
echo(_("Downloading ") + cbr[u'name'] + ", %0.2f%% " % round(
100 * float(bytes_so_far) / total_size, 2))
fd.flush()
echo(_("Downloading ") + cbr[u'name'] + _(" complete!"))
fd.close()
else:
echo(_("Can't open file"))
page += 1
else:
echo("Can't connect to the Cloud!")
break
time.sleep(0.1)
pdb.gimp_brushes_refresh()
register(
proc_name=("Brushes_Downloader"),
blurb=_(
"BRUSHES DOWNLOADER PLUGIN\n\n\nIf you have some questions about the plugin, write it here: \nhttp://www.zoonman.com/projects/gimp-plugins/brushes-downloader/ "),
help=("Download brushes."),
author=("Philipp Tkachev"),
copyright=("Philipp Tkachev"),
date=("2013"),
label=_("Download brushes"),
imagetypes=(""),
params=[
(PF_DIRNAME, "path", _("Save Brushes to this _Directory"), (gimp.directory + os.sep + 'brushes') ),
(PF_RADIO, "size", _("Size"), "",
((_('_Any'), ""), (_('_Small'), 'small'), (_('_Middle'), 'mid'), (_('_Big'), 'big'))),
],
results=[],
function=(download_brushes),
menu=("<Image>/Tools/GimpCloud"),
domain=("gimp20-python", gimp.locale_directory)
)
main()