Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle bytes more directly #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions nyc_subway_zoom.star
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ GTFS_FEED_URL = "https://api-endpoint.mta.info/Dataservice/mtagtfsfeeds/nyct%2Fg

# The ID of the stop to watch.
# For the NYC MTA, see: http://web.mta.info/developers/data/nyct/subway/google_transit.zip
STOP_ID = "123S"
STOP_ID = b"123S"

# The color of the bullet to use for arrivals.
BULLET_COLOR = "#ee352e"
Expand Down Expand Up @@ -57,7 +57,7 @@ def render_bullet(route):
return render.Circle(
color = BULLET_COLOR,
diameter = 7,
child = render.Text(route, font = "tom-thumb"),
child = render.Text(str(route), font = "tom-thumb"),
)

def render_eta(eta):
Expand Down Expand Up @@ -89,7 +89,7 @@ STOP_TIME_UPDATE_ARRIVAL_FIELD_NUMBER = 2
STOP_TIME_EVENT_ARRIVAL_TIME_FIELD_NUMBER = 2

def gtfs_get_feed(url):
return http.get(url, ttl_seconds = 5).body()
return http.get(url, ttl_seconds = 5).bytes()

def gtfs_get_upcoming_arrivals(stop_id, reader):
upcoming_arrivals = []
Expand All @@ -113,7 +113,6 @@ def gtfs_get_upcoming_arrivals(stop_id, reader):
arrival_time = time.from_timestamp(arrival_times[0])
if arrival_time > time.now():
upcoming_arrivals.append(struct(route_id = route_id, eta = arrival_time))

return sorted(upcoming_arrivals, key = lambda x: x.eta)

# ==> Generic Protobuf decoding.
Expand Down Expand Up @@ -178,6 +177,6 @@ def proto_decode_len(reader):
return out, reader

def proto_next_byte(reader):
out = reader.elem_ords()[0]
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some reason, elem_ords() actually returns the bytes values, while ord(reader[0]) when reader is a string returns 65533 for non-ASCII bytes.

out = ord(reader[0])
reader = reader[1:]
return out, reader