2017-07-17 7 views
-2

PythonでJsonをちょっと乱暴にしていましたが、私はtvmaze.comによって提供されたAPIを読んでみたかったのです。私は項目をうまく取得することができますが、リストは長いですが、私はローカルクロックから+1時間に制限したいと思います。たとえば、私のコンピュータの時計が15:00であると私には16:00のアイテムが表示されるので、15:30にそれは私に16:30のアイテムを表示します。 PHPでは、私は現在の時間に3,600秒を追加するが、Pythonでは、全体の時間 - デルタは混乱しているようだ。ここで現在の時刻の1時間後に一致するフィードのPythonプリントアイテム

私は現在

import urllib2 
import simplejson 

response = urllib2.urlopen("http://api.tvmaze.com/schedule?country=US&date=2017-07-17") 
data = simplejson.load(response) 
myList=["abc","amc","animal planet","bravo","cartoon network","cbs","comedy central","the cw","discovery channel","fox","freeform","hbo","history","mtv","nbc","nickelodeon","tbs","tnt","usa network","wwe network"] 
for post in data: 
    if post["show"]["network"]["name"].lower() in myList: 
     airtime = post["airtime"] 
     network = post["show"]["network"]["name"] 
     name = post["show"]["name"] 
     season = post["season"] 
     ep = post["number"] 
     time = post["show"]["schedule"]["time"] 
     date = post["airdate"] 
     #summary = ["show"]["summary"] 
     print airtime,"",name,"- Season",season,"EP",ep,"("+network+")" 

を使用していますPythonコードで出力例:

21:00 Preacher - Season 2 EP 5 (AMC) 
21:00 Will - Season 1 EP 3 (TNT) 
21:00 American Pickers - Season 17 EP 10 (History) 
21:00 Stitchers - Season 3 EP 6 (FreeForm) 
21:00 Superhuman - Season 1 EP 6 (FOX) 
21:00 Whose Line Is It Anyway? - Season 13 EP 6 (The CW) 
21:00 Teen Mom 2 - Season 8 EP 1 (MTV) 
21:00 Street Outlaws: New Orleans - Season 2 EP 4 (Discovery Channel) 
21:00 The Real Housewives of Orange County - Season 12 EP 2 (Bravo) 
21:00 Teen Mom - Season 7 EP 30 (MTV) 
21:00 Alaska: The Last Frontier: The Frozen Edge - Season 4 EP 13 (Animal Planet) 

答えて

1

あなたは簡単にあなたの現在の時刻を取得し、datetimeモジュールを使用して、それに任意のオフセットを追加することができ、例えば:今

import datetime 

current_datetime = datetime.datetime.now() # local date and time 
current_time = datetime.datetime.time(current_datetime) # time only 
plus_hour = datetime.datetime.time(current_datetime + datetime.timedelta(hours=1)) # +1h 

、あなたは、あなたが常にからデータを回すことができるように「フィルタ」したい場合あなたは、時間オブジェクトを呼び出すとしているAPIを比較するが、正確な時間のためにあなたのケースで必要なのは、例えば、時間とあなたのplus_hour時間にエントリの分を比較することです:

split_time = post["airtime"].split(":") # split the time field 
time_hour = time_minute = 0 # initialize as both as `0` 
if len(split_time) >= 2: 
    time_hour = int(split_time[0]) 
    time_minute = int(split_time[1]) 
elif len(split_time) > 0: 
    time_hour = int(split_time[0]) 
if int(time_hour) != plus_hour.hour and int(time_minute) != plus_hour.minute: 
    continue # no match, move to the next item... 
+0

私は間違いをしているので、明らかに間違っている。ValueError:もっと必要とする解凍する値は1より大きい。 – Slightz

+0

これは、 'post [" airtime "]'に有効な時間が含まれていないことを意味します。更新をチェックしてください。 – zwer

+0

私はどのように私のコードでそれを実装するのだろうか? ifとelifをforループの中に追加しますか? – Slightz

0

時間デルタは混乱に見えるかもしれませんが、それは仕事に最適なツールです。

ここでは、Pythonでtimedeltasを扱うときに私が通常取るアプローチです。うまくいけば、これはあなたにもっと読みやすくなります。

>>> from datetime import datetime, timedelta 
>>> now = datetime.utcnow() 
>>> def hour_past(dt): 
...  return dt + timedelta(hours=1) 
... 
>>> hour_from_now = hour_past(now) 
>>> hour_from_now > now 
True 

確かに、それはdatetimeのではなくtime秒でですが、それは私が個人的理由utcnow()の信頼性を好むものだけです。

関連する問題