Completed
Push — master ( 2741fe...8da546 )
by Anas
29s
created

sonyan_post()   A

Complexity

Conditions 4

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 4
c 3
b 0
f 0
dl 0
loc 13
rs 9.2
1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
3
from telegram.ext import CommandHandler
4
from telegram import InputMediaPhoto
5
import requests
6
import yaml
7
8
owner = "-98881019"
9
count = "1"
10
offset = 0
11
12
13
def module_init(gd):
14
    global channel, token, database
15
    channel = gd.config["channel"]
16
    token = gd.config["vk_token"]
17
    commands = gd.config["commands"]
18
    database = gd.config["database"]
19
    for command in commands:
20
        gd.dp.add_handler(CommandHandler(command, sonyan_post))
21
    jobQueue = gd.updater.job_queue
22
    jobQueue.run_repeating(callback=sonyan_post, interval=60, first=0, context="@"+channel, name='RepeatingJob')
23
24
25
26
def sonyan_post(bot, update):
27
    with open(database, "r") as datefile:
28
        date_old = yaml.load(datefile)["date"]
29
    date = check_post(owner, offset, count, token)
30
    if date > date_old:
31
        print("New post!")
32
        post_link, images = dlpic(owner, offset, count, token)
0 ignored issues
show
Unused Code introduced by
The variable post_link seems to be unused.
Loading history...
33
    else:
34
        return
35
    bot.send_media_group(chat_id="@"+channel, media=images)
36
    data = {"date" : date}
37
    with open(database, "w") as datefile:
38
        yaml.dump(data, datefile)
39
40
41
def check_post(owner, offset, count, token):
42
    wallposts = requests.get("https://api.vk.com/method/wall.get?"+
43
                            "owner_id="+owner+
44
                             "&offset="+str(offset)+
45
                             "&count="+count+
46
                             "&filter="+owner+
47
                             "&access_token="+token+
48
                             "&v=5.60")
49
    serverjson = wallposts.json()
50
    date = serverjson["response"]["items"][0]["date"]
51
    return date
52
53
54
def dlpic(owner, offset, count, token):
55
    wallposts = requests.get("https://api.vk.com/method/wall.get?"+
56
                             "owner_id="+owner+
57
                             "&offset="+str(offset)+
58
                             "&count="+count+
59
                             "&filter="+owner+
60
                             "&access_token="+token+
61
                             "&v=5.60")
62
    serverjson = wallposts.json()
63
    vk_id = serverjson["response"]["items"][0]["id"]
64
    post_link = "https://vk.com/wall-98881019_"+str(vk_id)
65
    try:
66
        attachments = serverjson["response"]["items"][0]["attachments"]
0 ignored issues
show
Unused Code introduced by
The variable attachments seems to be unused.
Loading history...
67
    except:
68
        return None, None, None, None
69
    media_list = []
70
    for i in serverjson["response"]["items"][0]["attachments"]:
71
        if "photo" not in i:
72
            continue
73
        photo = i["photo"]
74
        link = photo[max((x for x in photo if x.startswith("photo_")), key=lambda x: int(x.split("_")[1]))]
75
        media = InputMediaPhoto(media=link)
76
        media_list.append(media)
77
    return post_link, media_list
78