client_id = '5366344' client_secret = 'g8uyxFMdQcnL9Hak5hup' redirect_uri = 'https://devel0per.space/projects/vk_album/authorize'
class Form_Album(forms.Form): url_album = forms.CharField(widget=forms.Textarea(attrs={'class': 'form-control materail_input', 'rows': '1', 'style': 'resize:none'}))
def Authorize(request): if request.method == 'GET': code = request.GET.get('code') responce = (requests.get('https://oauth.vk.com/access_token?client_id='+client_id+'&client_secret='+client_secret+'&redirect_uri='+redirect_uri+'&code='+code)).json() token = responce['access_token'] return HttpResponseRedirect ('/projects/vk_album/')
# def Url_Manager(request): if request.method == 'POST': form = Form_Album(request.POST) # , . . if re.findall('album',request.POST.get('url_album')) == ['album']: # album url_album = re.sub('.*album','',request.POST.get('url_album')).split("_") # if url_album[1] == '0': photos_get = (requests.post('https://api.vk.com/method/photos.get?owner_id='+str(url_album[0])+'&album_id=profile&v=5.50')).json() return Download_Url(photos_get,url_album) # elif url_album[1] == '00?rev=1': photos_get = (requests.post('https://api.vk.com/method/photos.get?owner_id='+str(url_album[0])+'&album_id=wall&v=5.50')).json() return Download_Url(photos_get,url_album) # elif url_album[1] == '000': photos_get = (requests.post('https://api.vk.com/method/photos.get?owner_id='+str(url_album[0])+'&album_id=saved&v=5.50')).json() return Download_Url(photos_get,url_album) # elif re.match ('\d',str(url_album [1])) != None: photos_get = (requests.post('https://api.vk.com/method/photos.get?owner_id='+str(url_album[0])+'&album_id='+str(url_album[1])+'&v=5.50')).json() return Download_Url(photos_get,url_album) else: # , return render(request, 'vk_album.html', {'error': ' ','form': form}) else: # , return render(request, 'vk_album.html', {'error': ' ','form': form}) else: form = Form_Album() # return render(request, 'vk_album.html', {'form': form})
def Download_Url(photos_get,url_album): # download_dir = '/home/khramtsov/vk_album/'+url_album[0] os.mkdir(download_dir) # for slice in photos_get['response']['items']: if 'photo_1280' in slice: os.system('wget -P '+download_dir+' '+slice['photo_1280']) elif 'photo_807' in slice: os.system('wget -P '+download_dir+' '+slice['photo_807']) elif 'photo_604' in slice: os.system('wget -P '+download_dir+' '+slice['photo_604']) elif 'photo_130' in slice: os.system('wget -P '+download_dir+' '+slice['photo_130']) elif 'photo_75' in slice: os.system('wget -P '+download_dir+' '+slice['photo_75']) # zip=ZipFile(download_dir+'.zip',mode='w') for root, dirs, files in os.walk(download_dir): for file in files: zip.write(os.path.join(root,file),arcname=os.path.join('Album', file)) zip.close() # os.system('rm -R '+download_dir) # response = HttpResponse(open(download_dir+'.zip', 'rb').read(),content_type='application/zip') response['Content-Disposition'] = 'attachment; filename='+url_album[1]+'.zip' return response
url(r'^projects/vk_album/$', Url_Manager, name='Form'), url(r'^projects/vk_album/download/$', Url_Manager, name='Form'), url(r'^projects/vk_album/authorize/$', Authorize, name='Authorize'),
<!-- , --> <p class="bg-danger text-center">{{ error }}</p> <!-- --> {{ form }}
Source: https://habr.com/ru/post/279977/
All Articles