Passed
Push — master ( 9bfda5...dc2491 )
by Anas
02:12
created

modules.vk.dlpic()   B

Complexity

Conditions 7

Size

Total Lines 22
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 22
nop 4
dl 0
loc 22
rs 7.952
c 0
b 0
f 0
1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
3
from telegram.ext import CommandHandler
4
from modules.logging import vk, vk_add
5
from telegram import InputMediaPhoto
6
import requests
7
import time
8
9
owner = "-98881019"
10
count = "1"
11
offset = 0
12
13
14
def module_init(gd):
15
    global channel, token, database
16
    channel = gd.config["channel"]
17
    token = gd.config["vk_token"]
18
    commands = gd.config["commands"]
19
    database = gd.config["database"]
20
    for command in commands:
21
        gd.dp.add_handler(CommandHandler(command, sonyan_post))
22
    jobQueue = gd.updater.job_queue
23
    jobQueue.run_repeating(callback=sonyan_post, interval=60, first=5, context="@"+channel, name='RepeatingJob')
24
25
26
def sonyan_post(bot, update):
27
    unixtime, post_id = check_post(owner, offset, count, token)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable token does not seem to be defined.
Loading history...
28
    current_time, unixtime_old = vk(bot, update)
29
    if unixtime > unixtime_old:
30
        print("New post!")
31
        images = dlpic(owner, offset, count, token)
32
    else:
33
        return
34
    bot.send_media_group(chat_id="@"+channel, media=images)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable channel does not seem to be defined.
Loading history...
35
    vk_add(bot, update, current_time, unixtime, post_id)
36
    time.sleep(10)
37
38
39
def check_post(owner, offset, count, token):
40
    wallposts = requests.get("https://api.vk.com/method/wall.get?"+
41
                            "owner_id="+owner+
42
                             "&offset="+str(offset)+
43
                             "&count="+count+
44
                             "&filter="+owner+
45
                             "&access_token="+token+
46
                             "&v=5.60")
47
    serverjson = wallposts.json()
48
    date = serverjson["response"]["items"][0]["date"]
49
    post_id = serverjson["response"]["items"][0]["id"]
50
    return date, post_id
51
52
53
def dlpic(owner, offset, count, token):
54
    wallposts = requests.get("https://api.vk.com/method/wall.get?"+
55
                             "owner_id="+owner+
56
                             "&offset="+str(offset)+
57
                             "&count="+count+
58
                             "&filter="+owner+
59
                             "&access_token="+token+
60
                             "&v=5.60")
61
    serverjson = wallposts.json()
62
    try:
63
        attachments = serverjson["response"]["items"][0]["attachments"]
64
    except:
65
        return None, None
66
    media_list = []
67
    for i in attachments:
68
        if "photo" not in i:
69
            continue
70
        photo = i["photo"]
71
        link = photo[max((x for x in photo if x.startswith("photo_")), key=lambda x: int(x.split("_")[1]))]
0 ignored issues
show
introduced by
The variable x does not seem to be defined in case the for loop on line 67 is not entered. Are you sure this can never be the case?
Loading history...
72
        media = InputMediaPhoto(media=link)
73
        media_list.append(media)
74
    return media_list
75