Completed
Push — master ( be0612...b37ba4 )
by Anas
30s
created

check_post()   A

Complexity

Conditions 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 11
rs 9.4285
1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
3
from telegram.ext import CommandHandler, Filters, Job, JobQueue
0 ignored issues
show
Unused Code introduced by
Unused Job imported from telegram.ext
Loading history...
Unused Code introduced by
Unused JobQueue imported from telegram.ext
Loading history...
Unused Code introduced by
Unused Filters imported from telegram.ext
Loading history...
4
from datetime import datetime
0 ignored issues
show
Unused Code introduced by
Unused datetime imported from datetime
Loading history...
5
from random import randint
6
import requests
7
import yaml
8
import os
0 ignored issues
show
Unused Code introduced by
The import os seems to be unused.
Loading history...
9
10
11
def handler(dp):
12
    dp.add_handler(CommandHandler("sonyan", sonyan_post))
13
14
with open("config.yml", "r") as f:
15
    access_token = yaml.load(f)["keys"]["vk_token"]
16
with open("config.yml", "r") as f:
17
    channel = yaml.load(f)["keys"]["channel"]
18
owner = "-98881019"
19
count = "1"
20
offset = 0
21
22
23
def sonyan_post(bot, update):
24
    with open("resources/date.yml", "r") as datefile:
25
        date_old = yaml.load(datefile)["date"]
26
    date = check_post(owner, offset, count, access_token)
27
28
    if date > date_old:
29
        print("New post!")
30
        post_link, filename, text, pics_amount = dlpic(owner, offset, count, access_token)
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are trying to unpack a non-sequence, which was defined at line 75.
Loading history...
Bug Best Practice introduced by
It seems like you are trying to unpack a non-sequence, which was defined at line 92.
Loading history...
31
    else:
32
        return
33
34
    if pics_amount == 1:
35
        images_text = text+"\n"+post_link
36
    else:
37
        images_text = text+"\n["+str(pics_amount)+" images] \n"+post_link
38
39
    with open("sonyan/"+filename, "rb") as file:
40
    	bot.sendPhoto(chat_id="@"+channel, photo=file, caption=images_text)
41
    data = {"date" : date}
42
    with open("resources/date.yml", "w") as datefile:
43
        yaml.dump(data, datefile)
44
45
46
def check_post(owner, offset, count, access_token):
47
    wallposts = requests.get("https://api.vk.com/method/wall.get?"+
48
                            "owner_id="+owner+
49
                             "&offset="+str(offset)+
50
                             "&count="+count+
51
                             "&filter="+owner+
52
                             "&access_token="+access_token+
53
                             "&v=5.60")
54
    serverjson = wallposts.json()
55
    date = serverjson["response"]["items"][0]["date"]
56
    return date
57
58
	
59
def dlpic(owner, offset, count, access_token):
60
    wallposts = requests.get("https://api.vk.com/method/wall.get?"+
61
                             "owner_id="+owner+
62
                             "&offset="+str(offset)+
63
                             "&count="+count+
64
                             "&filter="+owner+
65
                             "&access_token="+access_token+
66
                             "&v=5.60")
67
    serverjson = wallposts.json()
68
69
    id = serverjson["response"]["items"][0]["id"]
0 ignored issues
show
Bug Best Practice introduced by
This seems to re-define the built-in id.

It is generally discouraged to redefine built-ins as this makes code very hard to read.

Loading history...
70
    text = serverjson["response"]["items"][0]["text"]
71
    post_link = "https://vk.com/wall-98881019_"+str(id)
72
    try:
73
        attachments = serverjson["response"]["items"][0]["attachments"]
74
    except:
75
        return None
76
    pics_amount = len(attachments)
77
    pic_to_get = randint(0, pics_amount-1)
78
79
    try:
80
        pic_link = serverjson["response"]["items"][0]["attachments"][pic_to_get]["photo"]["photo_2560"]
81
    except:
82
        try:
83
            pic_link = serverjson["response"]["items"][0]["attachments"][pic_to_get]["photo"]["photo_1280"]
84
        except:
85
            try:
86
                pic_link = serverjson["response"]["items"][0]["attachments"][pic_to_get]["photo"]["photo_807"]
87
            except:
88
                try:
89
                    pic_link = serverjson["response"]["items"][0]["attachments"][pic_to_get]["photo"]["photo_604"]
90
                except:
91
                    print("can't get link!")
92
                    return None
93
94
    dl = requests.get(pic_link)
95
    with open("sonyan/"+pic_link[-13:], "wb") as code:
96
        code.write(dl.content)
97
98
    return post_link, pic_link[-13:], text, pics_amount
99