Created
December 17, 2016 15:38
-
-
Save falms/ae37a10c326e0ba39a3cd62c7e523f3d to your computer and use it in GitHub Desktop.
Sync Chinachu to nasne
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import json | |
import re | |
import base64 | |
def base36encode(number): | |
alphabet, base36 = ['0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ', ''] | |
while number: | |
number, i = divmod(number, 36) | |
base36 = alphabet[i] + base36 | |
return base36 or alphabet[0] | |
def generateProgID(service_id, event_id): | |
return "gr{}-{}".format(service_id, base36encode(event_id).lower()) | |
def extractServiceIdEventId(program_id): | |
m = re.compile(r'([a-z]{2})(\d+)-(\w+)').match(program_id) | |
return [m.group(1), int(m.group(2)), int(m.group(3), 36)] | |
nasne_host = "192.168.0.101" | |
chinachu_api = "http://192.168.0.102:10772/api/" | |
s = requests.Session(); | |
# nasne予約リスト読み込み | |
res = s.get("http://" + nasne_host + ":64220/schedule/reservedListGet?searchCriteria=0&filter=0&startingIndex=0&requestedCount=0&sortCriteria=1") | |
reserved_nasne = json.loads(res.text) | |
# Chinachu予約リスト読み込み | |
res = s.get(chinachu_api + "reserves.json") | |
reserved_chinachu = json.loads(res.text) | |
# nasne,Chinachuの予約数を表示 | |
print(str(len(reserved_nasne['item'])) + " items in nasne") | |
print(str(len(reserved_chinachu)) + " items in Chinachu") | |
# Chinachuに登録されていない番組リストを作成 | |
add_items = [] | |
for item_from in reserved_nasne['item']: | |
# 番組情報が見つからない予約は無視する | |
if item_from['eventId']==65536: | |
continue | |
exists = False | |
for item_to in reserved_chinachu: | |
ch_type, service_id, event_id = extractServiceIdEventId(item_to['id']) | |
if item_from['serviceId'] == service_id and item_from['eventId'] == event_id: | |
exists = True | |
break | |
if exists == False: | |
add_items.append(item_from) | |
# Chinachuに登録されていない番組を予約する | |
for add_item in add_items: | |
program_id = generateProgID(add_item['serviceId'], add_item['eventId']) | |
s.put(chinachu_api + "program/" + program_id + ".json") | |
# 追加した予約数を表示 | |
print(str(len(add_items)) + " items added to Chinachu") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment