-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.py
161 lines (127 loc) · 5 KB
/
main.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import StringIO
import json
import logging
import random
import urllib
import urllib2
import time
# parsing plex data from xml api results
from xml.dom import minidom
from xml.etree.ElementTree import *
# for sending images
from PIL import Image
import multipart
# standard app engine imports
from google.appengine.api import urlfetch
from google.appengine.ext import ndb
import webapp2
TOKEN = 'TELEGRAM BOT API HERE!'
BASE_URL = 'https://api.telegram.org/bot' + TOKEN + '/'
# ================================
class EnableStatus(ndb.Model):
# key name: str(chat_id)
enabled = ndb.BooleanProperty(indexed=False, default=False)
# ================================
def setEnabled(chat_id, yes):
es = EnableStatus.get_or_insert(str(chat_id))
es.enabled = yes
es.put()
def getEnabled(chat_id):
es = EnableStatus.get_by_id(str(chat_id))
if es:
return es.enabled
return False
# ================================
class MeHandler(webapp2.RequestHandler):
def get(self):
urlfetch.set_default_fetch_deadline(60)
self.response.write(json.dumps(json.load(urllib2.urlopen(BASE_URL + 'getMe'))))
class GetUpdatesHandler(webapp2.RequestHandler):
def get(self):
urlfetch.set_default_fetch_deadline(60)
self.response.write(json.dumps(json.load(urllib2.urlopen(BASE_URL + 'getUpdates'))))
class SetWebhookHandler(webapp2.RequestHandler):
def get(self):
urlfetch.set_default_fetch_deadline(60)
url = self.request.get('url')
if url:
self.response.write(json.dumps(json.load(urllib2.urlopen(BASE_URL + 'setWebhook', urllib.urlencode({'url': url})))))
class WebhookHandler(webapp2.RequestHandler):
def post(self):
urlfetch.set_default_fetch_deadline(60)
body = json.loads(self.request.body)
logging.info('request body:')
logging.info(body)
self.response.write(json.dumps(body))
update_id = body['update_id']
message = body['message']
message_id = message.get('message_id')
date = message.get('date')
text = message.get('text')
fr = message.get('from')
chat = message['chat']
chat_id = chat['id']
if not text:
logging.info('no text')
return
def reply(msg=None, img=None):
if msg:
resp = urllib2.urlopen(BASE_URL + 'sendMessage', urllib.urlencode({
'chat_id': str(chat_id),
'text': msg.encode('utf-8'),
'disable_web_page_preview': 'true',
#'reply_to_message_id': str(message_id),
})).read()
elif img:
resp = multipart.post_multipart(BASE_URL + 'sendPhoto', [
('chat_id', str(chat_id)),
('reply_to_message_id', str(message_id)),
], [
('photo', 'image.jpg', img),
])
else:
logging.error('no msg or img specified')
resp = None
logging.info('send response:')
logging.info(resp)
#I CUSTOMISED FROM HERE!!!
if '/tv' in text:
purl = urllib2.Request(url='http://plex_server_address_or_IP_address_here:32400/library/sections/1/newest?X-Plex-Token=YOUR_PLEX_TOKEN_HERE')
f = urllib2.urlopen(purl)
tree = ElementTree()
tree.parse(f)
root = tree.getroot()
shows = root.findall('./Video')
for s in shows[:10]:
title = s.attrib['grandparentTitle']
season = s.attrib['title']
#added = s.attrib['addedAt'] wanted to add the date and time but not sure how to convert Epoch time format.
reply(title +' - '+ season)
if '/movies' in text:
purl = urllib2.Request(url='http://plex_server_address_or_IP_address_here:32400/library/sections/2/recentlyAdded?X-Plex-Token=YOUR_PLEX_TOKEN_HERE')
f = urllib2.urlopen(purl)
tree = ElementTree()
tree.parse(f)
root = tree.getroot()
shows = root.findall('./Video')
for s in shows[:5]:
title = s.attrib['title']
summary = s.attrib['summary']
reply(title +' - '+ summary)
if '/anime' in text:
purl = urllib2.Request(url='http://plex_server_address_or_IP_address_here:32400/library/sections/6/newest?X-Plex-Token=YOUR_PLEX_TOKEN_HERE')
f = urllib2.urlopen(purl)
tree = ElementTree()
tree.parse(f)
root = tree.getroot()
shows = root.findall('./Video')
for s in shows[:5]:
title = s.attrib['grandparentTitle']
season = s.attrib['title']
reply(title +' - '+ season)
app = webapp2.WSGIApplication([
('/me', MeHandler),
('/updates', GetUpdatesHandler),
('/set_webhook', SetWebhookHandler),
('/webhook', WebhookHandler),
], debug=True)