
はじめに
少し前までは、コミュニティアルバムからすべての画像をダウンロードし、インターネットで適切なサービスを検索したいという要望がありましたが、有料サービスのみに遭遇しました。
そして彼の誕生日の前夜には、サービスを書く時間と素晴らしい気分がありました。
開発のために、Pythonを使用し、Djangoの既存のプロジェクト用のアプリケーションを作成することにしました。 行きましょう。
VKontakteアプリケーションを作成し 、サービスがホストされるサイトを示します。 そして、アプリケーションデータを取得し、それをアプリケーションに書き込みます。
VKontakteアプリケーションのソースデータ。
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'}))
ユーザーを承認します。photos.getメソッドを使用するため、受け取ったトークンは必要ありません。 承認後、ユーザーをフォームにリダイレクトするだけです。
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を追加します。
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'),
そして今、すべての準備ができたら、フォームでhtmlページを作成します。
<!-- , --> <p class="bg-danger text-center">{{ error }}</p> <!-- --> {{ form }}
おわりに
その結果、Vkontakteアルバムから画像をダウンロードするための独自のサービスが提供され、その機能を補完できます。 たとえば、統計の収集など。
Github
デモ