Skip to content

Commit

Permalink
ff2mpv.py: Add Python native client (woodruffw#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
kirill9000 authored and woodruffw committed Nov 28, 2017
1 parent c5a955e commit 9a6150d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ well**!

First, install the addon from [AMO](https://addons.mozilla.org/en-US/firefox/addon/ff2mpv/).

Then, copy `ff2mpv` (the Ruby script) to somewhere of your choice. Make sure it's executable.
Then, copy either `ff2mpv` (the Ruby script) or `ff2mpv.py` (the Python script) to somewhere of your choice. Make sure it's executable.

Finally, copy `ff2mpv.json` (in this repository) into `~/.mozilla/native-messaging-hosts/`. Open
it in your editor, and change the `path` field to correspond to the path where you saved
the `ff2mpv` script.
the script of your choice.

After that, everything should work.

Expand Down
34 changes: 34 additions & 0 deletions ff2mpv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env python3
import sys, struct, json
from subprocess import Popen, DEVNULL

def main():
message = get_message()
url = message.get('url')
args = ['mpv', '--no-terminal', '--', url]
Popen(args, stdin=DEVNULL, stdout=DEVNULL, stderr=DEVNULL)
# Need to respond something to avoid "Error: An unexpected error occurred"
# in Browser Console.
send_message('ok')


# https://developer.mozilla.org/en-US/Add-ons/WebExtensions/Native_messaging#App_side
def get_message():
raw_length = sys.stdin.buffer.read(4)
if not raw_length:
return {}
length = struct.unpack('@I', raw_length)[0]
message = sys.stdin.buffer.read(length).decode('utf-8')
return json.loads(message)


def send_message(message):
content = json.dumps(message).encode('utf-8')
length = struct.pack('@I', len(content))
sys.stdout.buffer.write(length)
sys.stdout.buffer.write(content)
sys.stdout.buffer.flush()


if __name__ == '__main__':
main()

0 comments on commit 9a6150d

Please sign in to comment.