Suppose there is a group in Vkontakte about the movie, which publishes announcements of new films and some other information. It is necessary to prepare posts in advance and publish according to the schedule. For this, I am going to use the Trello board, where the posts and Vkontakte API are stored to publish these posts in a group.
Let's create a board in Trello with the name "Cinema", and in it two sheets of "New Week" and "Soon at the box office". Add there 4 cards, 2 with the publication for today, 1 for tomorrow and 1 without a date.
To work with the Trello API and Vkontakte API, we will need the following libraries.
$ pip3 install py-trello $ pip3 install py-vkontakte
KEY and TOKEN get here https://trello.com/app-key
import trello client = trello.TrelloClient(api_key=KEY, token=TOKEN) board = client.get_board(ID) # https://trello.com/b/nC8QJJoZ/, ID — nC8QJJoZ
With the help of "Due Date" in the Trello card we set the time when it is necessary to publish it on Vkontakte.
def is_card_can_published(trello_card): if not trello_card.due_date: return False now_unixtime = datetime.datetime.utcnow().replace(tzinfo=None).timestamp() card_due_unixtime = trello_card.due_date.replace(tzinfo=None).timestamp() delta = card_due_unixtime - now_unixtime return True if delta <= 0 else False card_items = (card for card in board.open_cards() if is_card_can_published(card))
The publication schedule can be viewed using the Trello internal calendar.
You can attach any file to the card, so first check the extension of the file. The function get_attachment_in_card
returns a tuple "file name" and "image byte string", which are used to load in VK.
import os import requests def is_image_file(attachment_url): _, file_extension = os.path.splitext(attachment_url) if file_extension not in ('.jpg', '.gif', '.png'): return False return True def download_attachment(attachment_url): response = requests.get(attachment_url, stream=True) return response.content def get_attachment_in_card(card): for attachment in card.get_attachments(): if not is_image_file(attachment.url): continue binary_content = download_attachment(attachment.url) _, filename = os.path.split(attachment.url) yield (filename, binary_content)
https://vk.com/dev/authcode_flow_user
>>> import vk >>> vk.create_url_get_code("ID_", 'https://oauth.vk.com/blank.html', scope='wall, photos') # CODE >>> vk.create_access_token("ID_", "_", "https://oauth.vk.com/blank.html", "CODE") # ACCESS_TOKEN
Get the VK group via py-vkontakte
import vk vk.set_access_token(ACCESS_TOKEN) group = vk.get_group(ID) # vk.com/apiclub, ID — apiclub vk.com/club1, ID — 1
Use our "get_attachment_in_card"
function to get the image from the Trello card. We load images in VK and we create the publication in group. After the post is published in Vkontakte, "due date" is marked as completed.
from vk.photos import Photo for card in card_items: attachment_items = \ {filename: binary_content for filename, binary_content in get_attachment_in_card(card)} photo_items = Photo.upload_wall_photos_for_group(group.id, attachment_items.items()) group.wall_post(message=card.name + '\n' + card.description, attachments=photo_items) card.set_due_complete()
→ GitHub
Source: https://habr.com/ru/post/327216/
All Articles