Попробую совместить svn с git
[uimge.git] / uimgepy / libuimge / imagehost.py
blob72943603ee48eecb43a1fc3603969b441d432c6a
1 # -*- coding: utf-8 -*-
2 import libiu
3 from re import findall
4 from urllib import urlopen
6 def label(file, name):
7 from PIL import Image
8 from os import stat
9 img=Image.open(file)
10 size=stat(file).st_size/1024
11 title='%s %sx%s %s Kb' %(name,str(img.size[0]),str(img.size[1]),str(size) )
12 return title
14 def host_i_ipicture(send):
15 file_name,label_name,mode=send[0],send[1],send[2]
16 if not mode:
17 ihost={\
18 'host':'ipicture.ru', \
19 'post':'/Upload/', \
20 'name':'userfile',\
21 'cookie':''\
24 form_vaule = [\
25 ('uploadtype','1'),\
26 ('method','file'),\
27 ('file','upload'),\
28 ('thumb_resize_on','on'),('thumb_resize','200'),\
29 ('submit','"Загрузить"')\
31 elif mode:
32 ihost={\
33 'host':'ipicture.ru', \
34 'post':'/Upload/', \
35 'name':'userfile',\
36 'cookie':''\
39 form_vaule = [\
40 ('uploadtype','2'),\
41 ('method','url'),\
42 ('userurl[]',file_name),\
43 ('thumb_resize_on','on'),('thumb_resize','200'),\
44 ('submit','"Загрузить"')\
46 if label_name != None:
47 form_vaule.insert(-1,('string_small_on','on'))
48 form_vaule.insert(-1,('string_small', label(file_name,label_name) ))
50 reurl=libiu.send_file(file_name, ihost, form_vaule, (mode,None))
51 reurl=reurl.getheaders()[-5]
52 reurl=findall('(http://.*.html)',reurl[1])
53 url=findall('\[IMG\](http://.*)\[\/IMG\]',urlopen(reurl[0]).read())
54 url=[url[0],url[2]]
55 #return ihost,form_vaule
56 return url
58 def host_r_radikal(send):
59 file_name,label_name, mode=send[0],send[1],send[2]
60 ihost={\
61 'host':'www.radikal.ru', \
62 'post':'/action.aspx', \
63 'name':'F',\
64 'cookie':''\
67 form_vaule = [\
68 ('upload', 'yes'),\
69 ('VM','200'),\
70 ('CP','yes'),\
71 ('Submit', '')\
73 if label_name != None:
74 form_vaule.insert(-1,('VE','yes'))
75 form_vaule.insert(-1,('V', label(file_name,label_name) ))
77 if mode:
78 form_vaule.insert(1,('URLF',file_name))
79 url=libiu.send_file(file_name, ihost, form_vaule, (mode,None)).read()
80 url=findall('\[IMG\](http://.*.radikal.ru.*)\[/IMG\]',url)
81 return url
83 def host_s_imageshack(send):
84 file_name,label,mode=send[0],send[1],send[2]
85 ihost={\
86 'host':'imageshack.us', \
87 'post':'/', \
88 'name':'fileupload',\
89 'cookie':''\
92 form_vaule = [\
93 ('uploadtype', 'on'),\
94 ('Submit', '"host it!"')\
96 src=libiu.send_file(file_name, ihost, form_vaule, (None, mode)).read()
97 url=findall('value=\"(http://img.[\d]+?.imageshack.us/img[\d]+?/.*?/.*?)\"', src)
99 tumburl=url[0].split('.')
100 tumburl.insert(-1,'th')
101 urls=[url[0],'.'.join(tumburl)]
102 return urls
104 def host_t_tinypic(send):
105 file_name,label,mode=send[0],send[1],send[2]
106 ihost={\
107 'host':'s3.tinypic.com', \
108 'post':'/upload.php', \
109 'name':'the_file',\
110 'cookie':''\
113 form_vaule = [\
114 ('action', 'upload'),\
115 ('MAX_FILE_SIZE', '200000000'),\
116 ('action', 'upload'),\
117 ('Submit', '')\
120 src=libiu.send_file(file_name, ihost, form_vaule , (None, mode)).read()
121 reurl=findall('http://tinypic.com/view.php\?pic=.*?\&s=[\d]',src)
122 src=urlopen(reurl[0]).read()
123 url=findall('\[IMG\](http://i[\d]+?.tinypic.com/.*?)\[/IMG\]',src)
124 tumburl=url[0].split('.')
125 tumburl[-2] += '_th'
126 tumburl = '.'.join(tumburl)
127 urls=[url[0],tumburl]
128 return urls
130 def host_m_smages(send):
131 from re import sub
132 file_name,label,mode=send[0],send[1],send[2]
133 ihost={\
134 'host':'smages.com', \
135 'post':'/upload', \
136 'name':'img',\
137 'cookie':''\
140 form_vaule = [('Submit', '')]
141 reurl = sub('(\/code\/)|(\.htm)','', libiu.send_file(file_name, ihost, form_vaule , (None, mode) ).getheaders()[4][1])
142 url,tmb = 'http://smages.com/i/%s'%reurl,'http://smages.com/t/%s'%reurl
143 return [url,tmb]
145 def host_u_funkyimg(send):
146 file_name,label,mode=send[0],send[1],send[2]
147 ihost={\
148 'host':'funkyimg.com', \
149 'post':'/up.php', \
150 'name':'file_0',\
151 'cookie':''\
153 form_vaule = [\
154 ('addInfo','on'),\
155 ('upload','"Upload Images"'),('uptype','file'),\
156 ('file_1',''),('maxNumber','1'),('maxId','')
158 url=findall('\[IMG\](http://funkyimg.com/.*)\[/IMG\]\[/URL\]',\
159 libiu.send_file(file_name, ihost, form_vaule, (None,mode)).read())
160 url.reverse()
161 return url
163 def _host_avangard_foto_cod(send):
164 import urllib2
166 email='nanodesu@in-mail.ru'
167 passwd='splenchb'
168 username='nanodesu4'
169 album_id='7469727749'
170 host='avangard.photo.cod.ru'
172 def auth_id_cod(user,passwd):
173 cookes=str(urllib2.urlopen('https://id.cod.ru/auth?email=%s&password=%s' %(user,passwd)).info())
174 cookes=findall('(codsid=.+?;)[\s\S\w\W]*(auth=YES;)[\s\S\w\W]*(modified=\d+?;)', cookes)[0]
175 cookes='%s %s %s' %(cookes[0],cookes[1],cookes[2])
176 return cookes
178 def get_pages_urls(cookie,o):
180 def get_pages(i,cookie):
181 urlpage='http://avangard.photo.cod.ru/users/%s/%s/?page=%d' %(username,album_id,i)
182 req = urllib2.Request(urlpage)
183 req.add_header('Cookie', cookie)
184 return urllib2.urlopen(req).read()
186 url_data= get_pages(i,cookie)
187 i=findall('<td width="80%" align="center" class="f12">.*<b>(\d{1,})</b></td>',url_data)
189 if i and not o:
190 i=int(i[0])
191 url_data= get_pages(i,cookie)
192 urls = findall('(http://avangard.photo.cod.ru/photos.*/w100_.*)" alt=',url_data)
194 elif i and o:
195 i=int(i[0])
196 urls=[]
197 for i in range(o,i+1):
198 url_data = get_pages(i,cookie)
199 for url in findall('(http://avangard.photo.cod.ru/photos.*/w100_.*)" alt=',url_data):
200 urls.append(url)
202 elif not i:
203 url_data= get_pages(1,cookie)
204 urls = findall('(http://avangard.photo.cod.ru/photos.*/w100_.*)" alt=',url_data)
205 #print type(urls), len(urls)
206 return urls,i
208 def output_urls(urls):
209 out_url=[]
210 for url in urls:
211 tmb_100=url
212 urlre=url.split('/w100_')
213 tmb_400='%s/w400_%s'%(urlre[0],urlre[1])
214 direct_url = '%s/%s'%(urlre[0],urlre[1])
215 out_url.append((direct_url,tmb_400))
216 return out_url
218 def main(files):
219 mode = None
220 cookie=auth_id_cod(email,passwd)
221 ihost={\
222 'host':host, \
223 'post':'/upload/', \
224 'name':'photos[]',\
225 'cookie':cookie\
228 form_vaule = [\
229 ('action', 'photo_upload'),\
230 ('album_id',album_id),\
231 ('Submit', '')\
234 old_urls, o= get_pages_urls(cookie, None)
235 for file in files:
236 if libiu.send_file(file, ihost, form_vaule , (None, mode)).status == 302: pass
237 else: print 'error'
238 new_urls, o=get_pages_urls(cookie, o)
239 upload_urls = list( set(old_urls) ^ set(new_urls) )
240 return output_urls(upload_urls)
242 return main(send)