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

modules.instagram   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 64
dl 0
loc 73
rs 10
c 0
b 0
f 0
wmc 12

3 Functions

Rating   Name   Duplication   Size   Complexity  
B instagram() 0 21 6
A module_init() 0 15 4
A instagram_button() 0 20 2
1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
3
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, ChatAction
4
from telegram.ext import CommandHandler, MessageHandler, CallbackQueryHandler
5
from modules.utils import caption_filter, get_image, send_image
6
from modules.logging import log_command
7
import modules.instagram_filters
8
from datetime import datetime
9
import inspect
10
import os
11
12
13
def module_init(gd):
14
    global path, extensions, filters
15
    path = gd.config["path"]
16
    extensions = gd.config["extensions"]
17
    commands = gd.config["commands"]
18
    for command in commands:
19
        gd.dp.add_handler(MessageHandler(caption_filter("/"+command), instagram))
20
        gd.dp.add_handler(CommandHandler(command, instagram))
21
    gd.dp.add_handler(CallbackQueryHandler(instagram_button, pattern="(filt_)\w+"))
22
23
    filters = []
24
    all_funcs = inspect.getmembers(modules.instagram_filters, inspect.isfunction)
25
    for i in range(0, len(all_funcs)):
26
        if all_funcs[i][0].startswith("filt_"):
27
            filters.append(all_funcs[i][0])
28
29
30
def instagram(bot, update):
31
    global filename
32
    filename = datetime.now().strftime("%d%m%y-%H%M%S%f")
33
    try:
34
        extension = get_image(bot, update, path, filename)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable path does not seem to be defined.
Loading history...
35
    except:
36
        update.message.reply_text("I can't get the image! :(")
37
        return
38
    if extension not in extensions:
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable extensions does not seem to be defined.
Loading history...
39
        update.message.reply_text("Unsupported file, onii-chan!")
40
        return False
41
    instagram_key_list = []
42
    for i in filters:
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable filters does not seem to be defined.
Loading history...
43
        inst_filter = i
44
        instagram_key = InlineKeyboardButton(str(i)[5:], callback_data=",".join([inst_filter, extension]))
45
        instagram_key_list.append(instagram_key)
46
    row_split = lambda list, size, acc=[]: (row_split(list[size:], size, acc + [list[:size]]) if list else acc)
1 ignored issue
show
Comprehensibility Best Practice introduced by
The variable row_split does not seem to be defined.
Loading history...
47
    rows = row_split(instagram_key_list, 3)
48
    instagram_keyboard = rows
49
    instagram_reply_markup = InlineKeyboardMarkup(instagram_keyboard)
50
    update.message.reply_text("Available filters are:", reply_markup=instagram_reply_markup)
51
52
53
def instagram_button(bot, update):
54
    current_time = datetime.strftime(datetime.now(), "%d.%m.%Y %H:%M:%S")
55
    query = update.callback_query
56
    chosen_filter, extension = update.callback_query.data.split(",")
57
    filter_name = str(chosen_filter)[5:]
58
    user = query.from_user.username
59
    bot.editMessageText(text="%s selected: %s\nProcessing..."
60
                        % (user, filter_name),
61
                        chat_id=query.message.chat_id,
62
                        message_id=query.message.message_id)
63
    query.message.chat.send_action(ChatAction.UPLOAD_PHOTO)
64
    try:
65
        getattr(modules.instagram_filters, chosen_filter)(path, filename, extension)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable path does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable filename does not seem to be defined.
Loading history...
66
    except:
67
        raise Exception("Instagram error")
68
    send_image(query, path, filename+"-"+filter_name, extension)
69
    os.remove(path+filename+extension)
70
    os.remove(path+filename+"-"+filter_name+extension)
71
    print(current_time, ">", "/instagram", ">", user)
72
    log_command(bot, update, current_time, "instagram")
73