-
Notifications
You must be signed in to change notification settings - Fork 0
/
kodi-db.py
executable file
·60 lines (47 loc) · 1.42 KB
/
kodi-db.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
#!/usr/bin/env python
import sys
import json
import getopt
from pymongo import MongoClient
from datetime import datetime
class DB:
def __init__(self, sql_host, content, title):
self.sql_host = sql_host
self.client = MongoClient(self.sql_host)
self.db = self.client.kodi
json_data = {
"name" : content,
"label" : title,
"date" : datetime.now().strftime('%Y-%m-%d %H:%M:%S')
}
# Special output captured by Home-Assistant cli sensor
print("%s\n" % json.dumps(json_data))
if not self.contains(title):
self.write(json_data)
self.client.close()
def contains(self, title):
result=self.db.watched.movies.find({"label" : title})
return not result.count() == 0
def write(self, json_data):
result=self.db.watched.movies.insert_one(json_data)
def main(argv):
try:
opts, args = getopt.getopt(argv[1:], "s:c:t:", ["sql=", "content=", "title="])
except getopt.GetoptError:
sys.exit(2)
sql_host = None
content = None
title = None
for opt, arg in opts:
if opt in ("-c", "--content"):
content = arg
elif opt in ("-t", "--title"):
title = arg
elif opt in ("-s", "--sql"):
sql_host = arg
try:
DB(sql_host, content, title)
except:
pass
if __name__ == "__main__":
main(sys.argv)