Skip to content

Commit

Permalink
Switch from netifaces to ifaddr for get_local_ip (#128)
Browse files Browse the repository at this point in the history
* Use ifaddr for get_local_ip

..instead of netifaces (now that zeroconf is doing the same).

* Update requirements

..ifaddr instead of netifaces.

* Rework get_local_ip

..factoring cc ip into the guesswork.

* Make get_local_ip ipv agnostic
  • Loading branch information
theychx committed Sep 18, 2018
1 parent 937c015 commit 7a9e3dc
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
4 changes: 3 additions & 1 deletion catt/controllers.py
Expand Up @@ -96,7 +96,9 @@ def setup_cast(device_name, video_url=None, prep=None, controller=None, ytdl_opt
if video_url:
model_name = DEVICES_WITH_TWO_MODEL_NAMES.get(cast.model_name, cast.model_name)
cc_info = (cast.device.manufacturer, model_name)
stream = StreamInfo(video_url, model=cc_info, device_type=cast.cast_type, ytdl_options=ytdl_options)
stream = StreamInfo(
video_url, model=cc_info, host=cast.host, device_type=cast.cast_type, ytdl_options=ytdl_options
)

if controller:
if controller == "default":
Expand Down
25 changes: 19 additions & 6 deletions catt/stream_info.py
@@ -1,8 +1,9 @@
import ipaddress
import random
from pathlib import Path

import click
import netifaces
import ifaddr
import youtube_dl

from .util import guess_mime
Expand Down Expand Up @@ -32,10 +33,10 @@ class StreamInfoError(Exception):


class StreamInfo:
def __init__(self, video_url, model=None, device_type=None, ytdl_options=None):
def __init__(self, video_url, host=None, model=None, device_type=None, ytdl_options=None):
if "://" not in video_url:
self._local_file = video_url
self.local_ip = self._get_local_ip()
self.local_ip = self._get_local_ip(host)
self.port = random.randrange(45000, 47000)
self.is_local_file = True
else:
Expand Down Expand Up @@ -148,9 +149,21 @@ def set_playlist_entry(self, number):
else:
raise StreamInfoError("called on non-playlist")

def _get_local_ip(self):
interface = netifaces.gateways()["default"][netifaces.AF_INET][1]
return netifaces.ifaddresses(interface)[netifaces.AF_INET][0]["addr"]
def _get_local_ip(self, host):
for adapter in ifaddr.get_adapters():
for adapter_ip in adapter.ips:
aip = adapter_ip.ip[0] if isinstance(adapter_ip.ip, tuple) else adapter_ip.ip
try:
if not isinstance(ipaddress.ip_address(host), type(ipaddress.ip_address(aip))):
raise ValueError
except ValueError:
continue
ipt = [(ip, adapter_ip.network_prefix) for ip in (aip, host)]
catt_net, cc_net = [ipaddress.ip_network("%s/%s" % ip, strict=False) for ip in ipt]
if catt_net == cc_net:
return aip
else:
continue

def _get_stream_preinfo(self, video_url):
try:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -13,7 +13,7 @@
with open("README.rst") as readme_file:
readme = readme_file.read()

requirements = ["youtube-dl>=2017.3.15", "PyChromecast>=2.3.0", "Click>=5.0", "netifaces>=0.10.7", "requests>=2.18.4"]
requirements = ["youtube-dl>=2017.3.15", "PyChromecast>=2.3.0", "Click>=5.0", "ifaddr>=0.1.4", "requests>=2.18.4"]

test_requirements = [] # type: ignore

Expand Down

0 comments on commit 7a9e3dc

Please sign in to comment.