Skip to content

Instantly share code, notes, and snippets.

@sklyar
Created September 6, 2023 20:55
Show Gist options
  • Save sklyar/0827ad33415ee6bae73baf0bce9029bd to your computer and use it in GitHub Desktop.
Save sklyar/0827ad33415ee6bae73baf0bce9029bd to your computer and use it in GitHub Desktop.
This Go program automates the deletion of vk posts for a specified account. It fetches and deletes posts from the VK wall starting from a given offset until it reaches a specified end date. The program uses VK SDK and requires API token and owner ID as environment variables.
package main
import (
"fmt"
"log"
"os"
"strconv"
"time"
"github.com/SevereCloud/vksdk/v2/api"
)
const fetchLimit = 20
type Client struct {
sdk *api.VK
ownerId string
}
func NewClient(token string, ownerId string) *Client {
sdk := api.NewVK(token)
sdk.Limit = api.LimitUserToken
return &Client{
sdk: sdk,
ownerId: ownerId,
}
}
type Post struct {
Id int
Text string
Date time.Time
}
func (c *Client) WallGet(offset int) ([]Post, error) {
params := api.Params{
"owner_id": c.ownerId,
"count": fetchLimit,
"offset": offset,
}
var posts []Post
response, err := c.sdk.WallGet(params)
if err != nil {
return nil, err
}
for _, item := range response.Items {
posts = append(posts, Post{
Id: item.ID,
Text: item.Text,
Date: time.Unix(int64(item.Date), 0),
})
}
return posts, nil
}
func (c *Client) WallDelete(post Post) error {
res, err := c.sdk.WallDelete(api.Params{
"owner_id": c.ownerId,
"post_id": post.Id,
})
if err != nil {
return err
}
if res != 1 {
return fmt.Errorf("post #%d not deleted", post.Id)
}
return err
}
func main() {
apiToken := os.Getenv("VK_API_TOKEN")
if apiToken == "" {
log.Panic("api token must be filled")
return
}
vkOwnerID := os.Getenv("VK_OWNER_ID")
if vkOwnerID == "" {
log.Panic("vk owner id must be filled")
return
}
vkStartFrom := os.Getenv("VK_START_FROM")
if vkStartFrom == "" {
log.Panic("vk start from must be filled")
return
}
vkEndDateStr := os.Getenv("VK_END_DATE")
if vkEndDateStr == "" {
log.Panic("vk end date must be filled")
return
}
endDate, err := time.Parse("2006-01-02", vkEndDateStr)
if err != nil {
log.Panic(err)
return
}
if endDate.IsZero() {
log.Panic("vk end date must be filled")
return
}
if endDate.After(time.Now()) {
log.Panic("vk end date must be before now")
return
}
offset, err := strconv.Atoi(vkStartFrom)
if err != nil {
log.Panic(err)
return
}
if offset < 1 {
log.Panic("vk start from must be greater than 0")
return
}
client := NewClient(apiToken, vkOwnerID)
for n := 0; ; {
posts, err := client.WallGet(offset)
if err != nil {
log.Panic(err)
return
}
if len(posts) == 0 {
log.Println("No more posts")
break
}
for _, post := range posts {
n++
date := post.Date.Truncate(24 * time.Hour)
if date.Before(endDate) || date.Equal(endDate) {
log.Printf("Reached the end date, stopping.")
return
}
log.Printf("Deleting post #%d by %s\n", n, post.Date.Format("2006-01-02T15:04"))
err := client.WallDelete(post)
if err != nil {
log.Panic(err)
return
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment