Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
VK API wall.post example (Django 1.11.x, RQ)
# Import Python packages
import requests
# Import VK API
import vk
# Import Django packages
from django.contrib.sites.models import Site
from django.contrib.humanize.templatetags import humanize
# Import Django Global settings
from django.conf import settings
# Import App models
from .models import Tours
# Import plural template tags
from pages.templatetags.plural_day import plural_day
from pages.templatetags.plural_month import plural_month
# RQ job
from django_rq import job
# Post to VK wall
@job
def post_to_vk(obj):
# Init object
tour = Tours.objects.get(id=obj.id)
hotels = obj.hotels.all().order_by('cost')
# Prepare text
message = '☀️ 🌴 {}{} ✈️ {} на {} от {} руб.'.format(
tour.country.name,
', ' + tour.resort.name if tour.resort else '',
plural_month(tour.date),
plural_day(tour.duration),
humanize.intcomma(hotels[0].cost)
)
if tour.additional_info:
message += '\n\n'
message += '{}\n'.format(tour.additional_info)
message += '\n\n'
# Prepare hotels list
for hotel in hotels:
message += '🏡 {}{}{}{}{} руб.\n'.format(
hotel.name,
' ' + hotel.get_star_display() if hotel.star != '0' else '',
' 👍 ' if hotel.recommended else ', ',
hotel.get_food_display(),
humanize.intcomma(hotel.cost)
)
message += '\n\n'
if tour.transfer:
message += '{}\n'.format(tour.get_transfer_display())
if tour.departure_condition:
message += '{}\n'.format(tour.get_departure_condition_display())
if tour.price_condition:
message += '{}\n'.format(tour.get_price_condition_display())
message += '\n\n'
message += '📞 Звоните сейчас: +7 (812) 3-094-094\n'
message += '💬 Или оставьте заявку: vk.me/vturizme\n'
message += '🔥 Оплатите онлайн, не выходя из дома!\n'
message += '\n\n'
message += '⚠️ Цены актуальны на момент публикации предложения!\n'
message += '\n\n'
message += '🎉 Все туры вы можете купить онлайн по банковской карте ⚡ не выходя из дома ⚡ или в офисе в центре города! 😃\n'
message += '\n\n'
message += '#{}{} {}\n'.format(
tour.country.name,
' #' + tour.resort.name if tour.resort else '',
settings.HASHTAGS_LIST
)
# Prepare photo
photo_url = '{}{}'.format(settings.BASE_DIR, tour.image_vk.url)
# Init VK API
vk_session = vk.Session(access_token=settings.VK_ACCESS_TOKEN)
vk_api = vk.API(vk_session, v='5.69', lang='ru', timeout=10)
# Prepare VK Upload server
try:
vk_upload_server = vk_api.photos.getWallUploadServer(
group_id=settings.VK_GROUP_ID)
# Upload photo to server
vk_upload_photo = requests.post(
vk_upload_server['upload_url'],
files={'photo': open(photo_url, 'rb')}
).json()
# Save photo to server
vk_save_photo = vk_api.photos.saveWallPhoto(
group_id=settings.VK_GROUP_ID,
photo=vk_upload_photo['photo'],
server=vk_upload_photo['server'],
hash=vk_upload_photo['hash']
)
# Result photo URL
vk_upload_photo_result = 'photo{}_{}'.format(
vk_save_photo[0]['owner_id'],
vk_save_photo[0]['id']
)
# Post to wall
vk_api.wall.post(
owner_id='-' + settings.VK_GROUP_ID,
from_group=1,
signed=0,
message=message,
attachments=vk_upload_photo_result
)
except vk.api.VkAPIError:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.