📜 ⬆️ ⬇️

Development of service for downloading albums



Introduction


Not so long ago, there was a desire to download all the images from the community album; when I looked for a suitable service on the Internet, I came across only paid services, which did not quite fit me.

And on the eve of the birthday, there was a time and a great mood for writing a service.
')
For development, I took Python, and decided that I would write an application for an existing project on Django. So let's go.

We create the Vkontakte application , we specify the site where service will be placed. And we get the application data, which we write to our application.

Initial data of the Vkontakte application.

client_id = '5366344' client_secret = 'g8uyxFMdQcnL9Hak5hup' redirect_uri = 'https://devel0per.space/projects/vk_album/authorize' 

Let's create the main form for the user to enter the link to the album.

 class Form_Album(forms.Form): url_album = forms.CharField(widget=forms.Textarea(attrs={'class': 'form-control materail_input', 'rows': '1', 'style': 'resize:none'})) 

We will authorize the user, and since we use the photos.get method, we will not need the received token. After logging in, simply redirect the user to our form.

 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/') 

Add a form handler.

 #  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}) 

And the final touch, getting links and downloading. As well as the creation of an archive.

 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 

Add a url to our project:

  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'), 

And now when you have everything ready, make the html page with the form.

 <!--     ,       --> <p class="bg-danger text-center">{{ error }}</p> <!--   --> {{ form }} 

Conclusion


As a result, we have got our own service for downloading images from Vkontakte albums, which can be supplemented with its functionality. Like collecting statistics, and so on.

Github
Demo

Source: https://habr.com/ru/post/279977/


All Articles