This repository has been archived by the owner on Sep 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
utils.py
105 lines (92 loc) · 2.23 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import telebot
import time
import json
import re
import datetime
import os.path
from sortedcontainers import SortedDict
from datetime import datetime
from settings import BOT_TOKEN
# bot and admin chat id
bot = telebot.TeleBot(BOT_TOKEN)
adminid = bot.get_me()
uptime = datetime.now()
# check for presence of required json files
generateEmptyJson()
def generateEmptyJson():
jsonFiles = [
"previousblogposts",
"previouscyborgmatt",
"previousjasons",
"previousmagesunite",
"previoussirbelvedere",
"previouswykrhm",
"userlist",
"grouplist"
]
for file in jsonFiles:
filename = file + ".json"
# prevent overwriting of file if it already exists
if not os.path.isfile(filename):
f = open(filename, "w+")
f.writelines("{\n\n}")
f.close()
def getCID(message):
return message.chat.id
def getContent(text):
command = re.findall(r'/{1}\w+[@RadRetroRobot]*\s+(.*)', text.text, re.DOTALL)
if command:
whole = ""
for line in command:
if line == "":
whole += "\n"
else:
whole += line
return whole
else:
pass
def addUser(userID, userName, filename):
user = {
userID : userName
}
with open(filename + '.json') as f:
data = json.load(f)
data.update(user)
with open(filename + '.json', 'w') as f:
json.dump(data, f)
def loadjson(filename):
# make sure Json files are properly created before loading.
generateEmptyJson()
with open(filename + '.json') as f:
data = json.load(f)
if filename == "suggestion" or filename == 'todo':
data = SortedDict(data)
return data
def deljson(value, filename):
data = loadjson(filename)
for key in data.keys():
if key == value:
del data[key]
with open(filename + '.json', 'w') as f:
json.dump(data, f)
return True
elif data[key] == value:
del data[key]
with open(filename + '.json', 'w') as f:
json.dump(data, f)
return True
return False
def intime(message):
timeRange = time.mktime(datetime.now().timetuple())
if int(timeRange - message.date) < 10:
if message.forward_from == None:
return True
else:
return False
def addBlogPostInstantView(url):
prefix = 'https://t.me/iv?url='
postfix = '&rhash=ef812bf2b658b4'
instant_view_link = prefix + url + postfix
return instant_view_link