added video transcode and ibpy examples
--HG-- branch : sandbox
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||
<?eclipse-pydev version="1.0"?>
|
||||
|
||||
<pydev_project>
|
||||
<?eclipse-pydev version="1.0"?><pydev_project>
|
||||
|
||||
<pydev_property name="org.python.pydev.PYTHON_PROJECT_VERSION">python 2.7</pydev_property>
|
||||
<pydev_property name="org.python.pydev.PYTHON_PROJECT_INTERPRETER">Default</pydev_property>
|
||||
<pydev_pathproperty name="org.python.pydev.PROJECT_SOURCE_PATH">
|
||||
<path>/sandbox/src</path>
|
||||
<path>/${PROJECT_DIR_NAME}/ibpy</path>
|
||||
<path>/${PROJECT_DIR_NAME}/transcode</path>
|
||||
</pydev_pathproperty>
|
||||
</pydev_project>
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
#Mon Feb 11 22:12:56 CET 2008
|
||||
eclipse.preferences.version=1
|
||||
project.repository.kind=trac
|
||||
project.repository.url=http\://localhost\:8080/xmlrpc
|
||||
project.repository.url=https\://baldev.de/trac/bots
|
||||
|
||||
@@ -1,90 +0,0 @@
|
||||
# Copyright 2011 baloan
|
||||
# See LICENSE for details.
|
||||
|
||||
''' empty module '''
|
||||
|
||||
import logging
|
||||
from circuits import Component, Debugger, Event, handler, Timer
|
||||
from circuits.net.sockets import TCPServer, TCPClient
|
||||
from circuits.net.sockets import Close, Connect, Write, Read
|
||||
from circuits.tools import inspect, graph
|
||||
|
||||
LOG = logging.getLogger()
|
||||
|
||||
logging.basicConfig(level=logging.DEBUG,
|
||||
format='%(asctime)s %(levelname).4s %(message)s',
|
||||
datefmt='%H:%M:%S')
|
||||
|
||||
# definitions
|
||||
|
||||
class Tick(Event):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(Tick, self).__init__(*args, **kwargs)
|
||||
|
||||
|
||||
class AutoConnect(Component):
|
||||
def __init__(self, host, port, *args, **kwargs):
|
||||
super(AutoConnect, self).__init__(*args, **kwargs)
|
||||
self.host = host
|
||||
self.port = port
|
||||
self.tcp = None
|
||||
|
||||
def started(self, component, mode):
|
||||
LOG.info("Started %s %s", component, mode)
|
||||
self.tcp = TCPClient(channel=self.channel)
|
||||
self.tcp.register(self)
|
||||
print inspect(system)
|
||||
print graph(system)
|
||||
|
||||
def ready(self, e):
|
||||
LOG.info("ready %s", e)
|
||||
self.push(Connect(self.host, self.port), target=self.channel)
|
||||
|
||||
def connected(self, host, port):
|
||||
LOG.info("Connected %s %d", host, port)
|
||||
# implement any negotiation here
|
||||
|
||||
def error(self, data):
|
||||
LOG.info("Error [%s]", data)
|
||||
|
||||
def disconnected(self):
|
||||
LOG.info("Disconnected")
|
||||
self.tcp.unregister(self)
|
||||
self.tcp = None
|
||||
self.stop()
|
||||
|
||||
def stopped(self):
|
||||
Timer(1, Event(), target='start').register(self)
|
||||
|
||||
class Parser(Component):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(Parser, self).__init__(*args, **kwargs)
|
||||
self.buffer = ""
|
||||
|
||||
@handler("read", "hmpf", target="biw")
|
||||
def read_biw(self, data):
|
||||
blcks = data.split("\x0c")
|
||||
blcks[0].join(self.buffer)
|
||||
for blk in blcks[:-1]:
|
||||
flds = blk.split("\x00")
|
||||
self.push(Tick(flds))
|
||||
self.buffer = blcks[-1]
|
||||
|
||||
@handler("read", target="cos")
|
||||
def read_cos(self, data):
|
||||
blcks = data.split("\x0c")
|
||||
blcks[0].join(self.buffer)
|
||||
for blk in blcks[:-1]:
|
||||
flds = blk.split("\x00")
|
||||
self.push(Tick(flds))
|
||||
self.buffer = blcks[-1]
|
||||
|
||||
def tick(self, flds):
|
||||
LOG.info("Tick %s", "|".join(flds))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
system = AutoConnect("xapgu17a-uat.de.db.com", 2013, channel="biw") + TCPServer(8001) + Parser() + Debugger()
|
||||
system.run(sleep=0.5)
|
||||
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
# Copyright (c) 2011 Andreas Balogh
|
||||
# See LICENSE for details.
|
||||
|
||||
""" circuits.prc_simulator """
|
||||
|
||||
# imports
|
||||
|
||||
import logging
|
||||
|
||||
# constants
|
||||
|
||||
# globals
|
||||
|
||||
LOG = logging.getLogger()
|
||||
|
||||
# definitions
|
||||
|
||||
@@ -1,86 +0,0 @@
|
||||
# Copyright 2010 baloan
|
||||
# See LICENSE for details.
|
||||
|
||||
''' multiprocessing demo '''
|
||||
|
||||
# system imports
|
||||
|
||||
from circuits import Component, Event, Timer, handler, Debugger
|
||||
from math import sqrt
|
||||
import logging
|
||||
|
||||
# globals
|
||||
|
||||
LOG = logging.getLogger()
|
||||
|
||||
logging.basicConfig(level=logging.DEBUG,
|
||||
format='%(asctime)s.%(msecs)03d %(process)d:%(thread)d %(levelname).4s %(message)s',
|
||||
datefmt='%H:%M:%S')
|
||||
|
||||
# definitions
|
||||
|
||||
class Print(Event):
|
||||
'Print Event'
|
||||
|
||||
class Timeout(Event):
|
||||
'Timeout'
|
||||
|
||||
|
||||
class Input(Component):
|
||||
def started(self, component, mode):
|
||||
LOG.info('started %s %s', component, mode)
|
||||
for n in range(100):
|
||||
LOG.info('put %i', n)
|
||||
self.push(Event(n))
|
||||
LOG.info('worker done, ending')
|
||||
Timer(5, Timeout('msg'), "timeout").register(self)
|
||||
|
||||
def timeout(self, e):
|
||||
LOG.info("timeout %s", e)
|
||||
self.stop()
|
||||
|
||||
def stopped(self, e):
|
||||
pass
|
||||
|
||||
|
||||
class Filter(Component):
|
||||
def started(self, component, mode):
|
||||
LOG.info('started %s %s', component, mode)
|
||||
|
||||
def event(self, n):
|
||||
LOG.info('Filter:event %i', n)
|
||||
if self.is_prime(n):
|
||||
LOG.info('Filter: sending %i', n)
|
||||
self.push(Print(n))
|
||||
|
||||
def is_prime(self, n):
|
||||
if n < 2:
|
||||
return False
|
||||
if n in (2, 3):
|
||||
return True
|
||||
for i in range(2, int(sqrt(n)) + 1):
|
||||
if n % i == 0:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
class Output(Component):
|
||||
def started(self, component, mode):
|
||||
LOG.info('started %s %s', component, mode)
|
||||
|
||||
@handler('print')
|
||||
def onPrint(self, n):
|
||||
print '%i, ' % (n,),
|
||||
|
||||
|
||||
def runInline():
|
||||
master = Input()
|
||||
master += Filter()
|
||||
master += Output()
|
||||
master += Debugger()
|
||||
master.run()
|
||||
LOG.info('done.')
|
||||
|
||||
if __name__ == '__main__':
|
||||
runInline()
|
||||
|
||||
439
src/ibpy/api_coverage.py
Normal file
439
src/ibpy/api_coverage.py
Normal file
@@ -0,0 +1,439 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# #
|
||||
# This script attempts to determine how much of the TWS API is
|
||||
# available from IbPy.
|
||||
#
|
||||
# It's not meant as an example of correct use of the package, nor is
|
||||
# it an example of correct use of a programming language.
|
||||
#
|
||||
# #
|
||||
import logging
|
||||
import sys
|
||||
|
||||
from functools import partial
|
||||
from logging import DEBUG, INFO, WARN, ERROR
|
||||
from optparse import OptionParser
|
||||
from random import randint
|
||||
from time import sleep, strftime, time
|
||||
|
||||
from ib.ext.ComboLeg import ComboLeg
|
||||
from ib.ext.Contract import Contract
|
||||
from ib.ext.ExecutionFilter import ExecutionFilter
|
||||
from ib.ext.Order import Order
|
||||
from ib.ext.ScannerSubscription import ScannerSubscription
|
||||
from ib.lib.logger import logger as basicConfig
|
||||
from ib.opt import ibConnection, message
|
||||
|
||||
|
||||
error_msgs = {}
|
||||
order_ids = [0]
|
||||
tick_msgs = []
|
||||
short_sleep = partial(sleep, 1)
|
||||
long_sleep = partial(sleep, 10)
|
||||
generic_tick_keys = '100,101,104,106,165,221,225,236'
|
||||
|
||||
unseen_hints = {
|
||||
'OpenOrder': 'only works with existing order(s) before connecting',
|
||||
'RealtimeBar': 'only works during trading hours',
|
||||
'ReceiveFA': 'does not work with edemo account',
|
||||
'UpdateNewsBulletin': 'news bulletins may not be available',
|
||||
'ConnectionClosed': 'may work if TWS is closed during script',
|
||||
}
|
||||
|
||||
verbose_levels = {
|
||||
3: DEBUG,
|
||||
2: INFO,
|
||||
1: WARN,
|
||||
0: ERROR,
|
||||
}
|
||||
|
||||
|
||||
def format_error(msg):
|
||||
which = ('(in %s)' % error_msgs.get(msg)) if error_msgs.get(msg) else ''
|
||||
return '%8s: %s %s' % (msg.errorCode, msg.errorMsg, which)
|
||||
|
||||
|
||||
def format_default(msg):
|
||||
return ' %s' % msg
|
||||
|
||||
|
||||
msg_formatters = {
|
||||
'default': format_default,
|
||||
'Error': format_error,
|
||||
}
|
||||
|
||||
|
||||
def next_order_id():
|
||||
return order_ids[-1]
|
||||
|
||||
|
||||
def save_order_id(msg):
|
||||
order_ids.append(msg.orderId)
|
||||
|
||||
|
||||
def save_tick(msg):
|
||||
tick_msgs.append(msg)
|
||||
|
||||
|
||||
def gen_tick_id():
|
||||
i = randint(100, 10000)
|
||||
while True:
|
||||
yield i
|
||||
i += 1
|
||||
if sys.version_info[0] < 3:
|
||||
gen_tick_id = gen_tick_id().next
|
||||
else:
|
||||
gen_tick_id = gen_tick_id().__next__
|
||||
|
||||
|
||||
def make_contract(symbol):
|
||||
contract = Contract()
|
||||
contract.m_symbol = symbol
|
||||
contract.m_secType = 'STK'
|
||||
contract.m_exchange = 'SMART'
|
||||
contract.m_primaryExch = 'SMART'
|
||||
contract.m_currency = 'USD'
|
||||
contract.m_localSymbol = symbol
|
||||
return contract
|
||||
|
||||
|
||||
def make_order(limit_price):
|
||||
order = Order()
|
||||
order.m_minQty = 100
|
||||
order.m_lmtPrice = limit_price
|
||||
order.m_orderType = 'MKT'
|
||||
order.m_totalQuantity = 100
|
||||
order.m_action = 'BUY'
|
||||
return order
|
||||
|
||||
|
||||
def exec_filter(client_id):
|
||||
contract = make_contract('NVDA')
|
||||
filt = ExecutionFilter()
|
||||
filt.m_clientId = client_id
|
||||
filt.m_symbol = contract.m_symbol
|
||||
filt.m_secType = contract.m_secType
|
||||
filt.m_exchange = contract.m_exchange
|
||||
return filt
|
||||
|
||||
|
||||
def make_msg_counter(rec_map, unrec_map):
|
||||
for classes in list(message.registry.values()):
|
||||
for cls in [c for c in classes if True]:
|
||||
if not cls.__name__.endswith('Pre') and not cls.__name__.endswith('Post'):
|
||||
rec_map[cls] = []
|
||||
def counter(msg):
|
||||
cls = msg.__class__
|
||||
try:
|
||||
rec_map[cls].append(msg)
|
||||
except (KeyError,):
|
||||
unrec_map.setdefault(cls, []).append(msg)
|
||||
return counter
|
||||
|
||||
|
||||
def make_error_catcher(seq):
|
||||
def catcher(msg):
|
||||
seq.append(msg)
|
||||
return catcher
|
||||
|
||||
|
||||
def maybe_verbose(call):
|
||||
name = call.__name__
|
||||
def inner(connection, options):
|
||||
logging.info('Start %s', name)
|
||||
start = time()
|
||||
v = call(connection, options)
|
||||
finish = time()
|
||||
logging.info('Finish %s in %0.3f sec', name, finish - start)
|
||||
return v
|
||||
inner.__name__ = name
|
||||
return inner
|
||||
|
||||
|
||||
def catch_errors(call):
|
||||
def inner(connection, options):
|
||||
errors = []
|
||||
catcher = make_error_catcher(errors)
|
||||
connection.register(catcher, 'Error')
|
||||
call(connection, options)
|
||||
connection.unregister(catcher, 'Error')
|
||||
return errors
|
||||
inner.__name__ = call.__name__
|
||||
return inner
|
||||
|
||||
|
||||
def test_000(connection, options):
|
||||
connection.setServerLogLevel(5)
|
||||
connection.reqCurrentTime()
|
||||
connection.reqAccountUpdates(1, 'DF16165')
|
||||
connection.reqManagedAccts()
|
||||
connection.requestFA(connection.GROUPS)
|
||||
connection.replaceFA(connection.GROUPS, '')
|
||||
connection.reqIds(10)
|
||||
|
||||
|
||||
def test_001(connection, options):
|
||||
ticker_id = gen_tick_id()
|
||||
subscript = ScannerSubscription()
|
||||
subscript.numberOfRows(3)
|
||||
subscript.locationCode('STK.NYSE')
|
||||
connection.reqScannerSubscription(ticker_id, subscript)
|
||||
connection.reqScannerParameters()
|
||||
short_sleep()
|
||||
connection.cancelScannerSubscription(ticker_id)
|
||||
|
||||
|
||||
def test_002(connection, options):
|
||||
ticker_id = gen_tick_id()
|
||||
contract = make_contract('NVDA')
|
||||
connection.reqMktData(ticker_id, contract, generic_tick_keys, False)
|
||||
short_sleep()
|
||||
connection.cancelMktData(ticker_id)
|
||||
|
||||
|
||||
def test_003(connection, options):
|
||||
ticker_id = gen_tick_id()
|
||||
contract = make_contract('GOOG')
|
||||
connection.reqMktDepth(ticker_id, contract, 10)
|
||||
short_sleep()
|
||||
connection.cancelMktDepth(ticker_id)
|
||||
|
||||
|
||||
def test_004(connection, options):
|
||||
connection.reqAllOpenOrders()
|
||||
connection.reqAutoOpenOrders(True)
|
||||
connection.reqOpenOrders()
|
||||
connection.reqExecutions(0, exec_filter(options.clientid))
|
||||
|
||||
|
||||
def test_005(connection, options):
|
||||
connection.reqNewsBulletins(True)
|
||||
short_sleep()
|
||||
connection.cancelNewsBulletins()
|
||||
|
||||
|
||||
def test_006(connection, options):
|
||||
try:
|
||||
askprice = [m.price for m in tick_msgs
|
||||
if (getattr(m, 'price', None) is not None) and m.field == 2][0]
|
||||
except (IndexError,):
|
||||
askprice = 100.0
|
||||
order = make_order(askprice)
|
||||
if options.demo:
|
||||
connection.placeOrder(id=next_order_id(),
|
||||
contract=make_contract('NVDA'),
|
||||
order=order)
|
||||
# connection.exerciseOptions()
|
||||
contract = make_contract('NVDA')
|
||||
connection.reqContractDetails(3, contract)
|
||||
|
||||
|
||||
def test_007(connection, options):
|
||||
endtime = strftime('%Y%m%d %H:%M:%S')
|
||||
ticker_id = gen_tick_id()
|
||||
connection.reqHistoricalData(
|
||||
tickerId=ticker_id,
|
||||
contract=make_contract('INTC'),
|
||||
endDateTime=endtime,
|
||||
durationStr='2 D',
|
||||
barSizeSetting='30 mins',
|
||||
whatToShow='TRADES',
|
||||
useRTH=0,
|
||||
formatDate=1)
|
||||
short_sleep()
|
||||
connection.cancelHistoricalData(ticker_id)
|
||||
|
||||
|
||||
def test_008a(connection, options):
|
||||
c = Contract()
|
||||
c.m_exchange = 'IDEALPRO'
|
||||
c.m_symbol = 'MO'
|
||||
c.m_localSymbol = 'MO1C'
|
||||
c.m_secType = 'BAG'
|
||||
c.m_expiry = '200806'
|
||||
leg1 = ComboLeg()
|
||||
leg1.m_conId = 123
|
||||
leg1.m_ratio = 1
|
||||
leg1.m_exchange = 'ONE'
|
||||
leg1.m_action = 'SELL'
|
||||
leg2 = ComboLeg()
|
||||
leg2.m_conId = 125
|
||||
leg2.m_ratio = 100
|
||||
leg2.m_exchange = 'NYSE'
|
||||
leg2.m_action = 'BUY'
|
||||
c.m_comboLegs = [leg1, leg2]
|
||||
connection.reqMktData(1, c, generic_tick_keys, False)
|
||||
|
||||
|
||||
def test_008b(connection, options):
|
||||
def cb(*a, **b):
|
||||
pass
|
||||
connection.register(cb, 'ExecDetails')
|
||||
filtr = exec_filter(options.clientid)
|
||||
connection.reqExecutions(1, filtr)
|
||||
c = Contract()
|
||||
c.m_symbol = 'GOOG'
|
||||
c.m_secType = 'OPT'
|
||||
c.m_exchange = 'SMART'
|
||||
c.m_right = 'CALL'
|
||||
c.m_strike = 360.0
|
||||
c.m_expiry = '200806'
|
||||
connection.reqMktData(2, c, '', False)
|
||||
long_sleep()
|
||||
|
||||
|
||||
def test_009(connection, options):
|
||||
ticker_id = gen_tick_id()
|
||||
connection.reqRealTimeBars(ticker_id, make_contract('AAPL'), 5, 'TRADES', 0)
|
||||
short_sleep()
|
||||
|
||||
|
||||
def test_010(connection, options):
|
||||
connection.reqPositions()
|
||||
short_sleep()
|
||||
connection.cancelPositions()
|
||||
|
||||
|
||||
def test_011(connection, options):
|
||||
reqId = gen_tick_id()
|
||||
connection.reqAccountSummary(reqId, 'All', 'AccountType,NetLiquidation')
|
||||
short_sleep()
|
||||
connection.cancelAccountSummary(reqId)
|
||||
|
||||
|
||||
def test_999(connection, options):
|
||||
short_sleep()
|
||||
connection.eDisconnect()
|
||||
|
||||
def last_wait(connection, options):
|
||||
pass
|
||||
|
||||
|
||||
def name_count(value):
|
||||
if value.count(':') == 1:
|
||||
name, count = value.split(':')
|
||||
try:
|
||||
count = int(count)
|
||||
except (TypeError, ValueError,):
|
||||
count = 0
|
||||
else:
|
||||
name, count = value, 0
|
||||
return name, count
|
||||
|
||||
|
||||
def get_options():
|
||||
version = '%prog 0.1'
|
||||
parser = OptionParser(version=version)
|
||||
add = parser.add_option
|
||||
add('-d', '--demo', dest='demo', action='store_true',
|
||||
help='Server using demo account, safe for placing orders')
|
||||
add('-m', '--messages', dest='printmsgs', action='store_true',
|
||||
help='Print message type names and exit')
|
||||
add('-s', '--show', dest='showmsgs', metavar='MSG[:MAX]', action='append',
|
||||
help=('Print no more than MAX messages of type MSG, may use ALL to '
|
||||
'print all messages, may be repeated'), default=[])
|
||||
add('-n', '--host', dest='host', default='localhost',
|
||||
help='Name or address of remote server (default: %default)')
|
||||
add('-p', '--port', dest='port', default=7496, type='int',
|
||||
help='Port number for remote connection (default: %default)')
|
||||
add('-c', '--client', dest='clientid', metavar='ID', default=0, type='int',
|
||||
help='Client id for remote connection (default: %default)')
|
||||
add('-v', '--verbose', default=0, action='count',
|
||||
help='Verbose output, may be repeated')
|
||||
opts, args = parser.parse_args()
|
||||
return opts
|
||||
|
||||
|
||||
def main(options):
|
||||
basicConfig()
|
||||
logging.root.setLevel(verbose_levels.get(options.verbose, ERROR))
|
||||
|
||||
rec_msgs = {}
|
||||
unrec_msgs = {}
|
||||
|
||||
handler = make_msg_counter(rec_msgs, unrec_msgs)
|
||||
|
||||
# # make_msg_counter fills in the defaults for the rec_msgs dict; now we can
|
||||
# # print those values and exit if the option is given
|
||||
if options.printmsgs:
|
||||
for name in sorted(k[0].typeName for k in list(rec_msgs.keys())):
|
||||
print(name)
|
||||
return
|
||||
|
||||
# # if we're still here, we should connect
|
||||
con = ibConnection(options.host, options.port, options.clientid)
|
||||
con.registerAll(handler)
|
||||
con.register(save_order_id, 'NextValidId')
|
||||
con.register(save_tick, 'TickSize', 'TickPrice')
|
||||
con.connect()
|
||||
short_sleep()
|
||||
|
||||
# # and if we've connected, we shoud execute all of the test functions in
|
||||
# # the module namespace.
|
||||
calls = [v for k, v in list(globals().items()) if k.startswith('test_')]
|
||||
for call in sorted(calls, key=lambda f: f.__name__):
|
||||
call = maybe_verbose(catch_errors(call))
|
||||
errors = call(con, options)
|
||||
for err in errors:
|
||||
error_msgs[err] = call.__name__
|
||||
|
||||
type_count = len(rec_msgs)
|
||||
seen_items = list(rec_msgs.items())
|
||||
seen = [(k, v) for k, v in seen_items if v]
|
||||
unseen = [(k, v) for k, v in seen_items if not v]
|
||||
|
||||
# # adjust the showmsgs option if given --show=all
|
||||
alls = [v for v in options.showmsgs if 'all' in v.lower()]
|
||||
if any(alls):
|
||||
all, count = name_count(alls[0])
|
||||
options.showmsgs = ['%s:%s' % (k.typeName, count) for k in list(rec_msgs.keys())]
|
||||
|
||||
# # ready, set, print!
|
||||
for msg_typename in options.showmsgs:
|
||||
msg_typename, msg_showmax = name_count(msg_typename)
|
||||
formatter = msg_formatters.get(msg_typename, msg_formatters['default'])
|
||||
msgs = [v for k, v in seen_items if k.typeName == msg_typename]
|
||||
if msgs:
|
||||
msgs = msgs[0]
|
||||
if not msg_showmax or msg_showmax > len(msgs):
|
||||
msg_showmax = len(msgs)
|
||||
print('\n%s (%s of %s):' % (msg_typename, msg_showmax, len(msgs),))
|
||||
for msg in msgs[0:msg_showmax]:
|
||||
print(formatter(msg))
|
||||
else:
|
||||
if msg_typename in [k.typeName for k in list(rec_msgs.keys())]:
|
||||
print('\n%s (%s):' % (msg_typename, 0,))
|
||||
else:
|
||||
print('\nMessage type %s not recognized' % (msg_typename,))
|
||||
|
||||
# # but wait, there's more! here we print a summary of seen message
|
||||
# # types and associated counts.
|
||||
if seen:
|
||||
print('\nSeen Message Types (count):')
|
||||
for cls, seq in sorted(seen, key=lambda t: t[0].typeName):
|
||||
print(' %s (%s)' % (cls.__name__, len(seq),))
|
||||
else:
|
||||
print('\nTotal failure; no messages received.')
|
||||
# # but wait, there's more! here we print a summary of unseen message
|
||||
# # types and associated counts.
|
||||
if unseen:
|
||||
print('\nUnseen Message Types (help):')
|
||||
for cls, zero in sorted(unseen, key=lambda t: t[0].typeName):
|
||||
name = cls.__name__
|
||||
help = unseen_hints.get(name, '')
|
||||
print(' %s%s' % (name, ' (%s)' % help if help else '',))
|
||||
else:
|
||||
print('\nAll Message types received.')
|
||||
# # last but not least we print the seen and unseen totals, and their ratio
|
||||
print('\nSummary:')
|
||||
args = (type_count, len(seen), len(unseen), 100 * len(seen) / float(type_count))
|
||||
print(' total:%s seen:%s unseen:%s coverage:%2.2f%%' % args)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
try:
|
||||
main(get_options())
|
||||
except (KeyboardInterrupt,):
|
||||
print('\nKeyboard interrupt.\n')
|
||||
61
src/ibpy/fancy_marketdata.py
Normal file
61
src/ibpy/fancy_marketdata.py
Normal file
@@ -0,0 +1,61 @@
|
||||
#! /usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from ib.ext.Contract import Contract
|
||||
from ib.opt import ibConnection, message
|
||||
from time import sleep
|
||||
|
||||
|
||||
# print all messages from TWS
|
||||
def watcher(msg):
|
||||
print(msg)
|
||||
|
||||
|
||||
# show Bid and Ask quotes
|
||||
def my_BidAsk(msg):
|
||||
if msg.field == 1:
|
||||
print('%s:%s: bid: %s' % (contractTuple[0],
|
||||
contractTuple[6], msg.price))
|
||||
elif msg.field == 2:
|
||||
print('%s:%s: ask: %s' % (contractTuple[0], contractTuple[6], msg.price))
|
||||
|
||||
|
||||
def makeStkContract(contractTuple):
|
||||
newContract = Contract()
|
||||
newContract.m_symbol = contractTuple[0]
|
||||
newContract.m_secType = contractTuple[1]
|
||||
newContract.m_exchange = contractTuple[2]
|
||||
newContract.m_currency = contractTuple[3]
|
||||
newContract.m_expiry = contractTuple[4]
|
||||
newContract.m_strike = contractTuple[5]
|
||||
newContract.m_right = contractTuple[6]
|
||||
print('Contract Values:%s,%s,%s,%s,%s,%s,%s:' % contractTuple)
|
||||
return newContract
|
||||
|
||||
if __name__ == '__main__':
|
||||
con = ibConnection('localhost', 4001)
|
||||
con.registerAll(watcher)
|
||||
showBidAskOnly = True # set False to see the raw messages
|
||||
if showBidAskOnly:
|
||||
con.unregister(watcher, message.tickSize, message.tickPrice,
|
||||
message.tickString, message.tickOptionComputation)
|
||||
con.register(my_BidAsk, message.tickPrice)
|
||||
con.connect()
|
||||
sleep(1)
|
||||
tickId = 1
|
||||
|
||||
# Note: Option quotes will give an error if they aren't shown in TWS
|
||||
contractTuple = ('GOOG', 'STK', 'SMART', 'USD', '', 0.0, '')
|
||||
# contractTuple = ('QQQQ', 'OPT', 'SMART', 'USD', '20070921', 47.0, 'CALL')
|
||||
# contractTuple = ('ES', 'FUT', 'GLOBEX', 'USD', '200709', 0.0, '')
|
||||
# contractTuple = ('ES', 'FOP', 'GLOBEX', 'USD', '20070920', 1460.0, 'CALL')
|
||||
# contractTuple = ('EUR', 'CASH', 'IDEALPRO', 'USD', '', 0.0, '')
|
||||
stkContract = makeStkContract(contractTuple)
|
||||
print('* * * * REQUESTING MARKET DATA * * * *')
|
||||
con.reqMktData(tickId, stkContract, '', False)
|
||||
sleep(15)
|
||||
print('* * * * CANCELING MARKET DATA * * * *')
|
||||
con.cancelMktData(tickId)
|
||||
sleep(1)
|
||||
con.disconnect()
|
||||
sleep(1)
|
||||
90
src/ibpy/ib_api_demo.py
Normal file
90
src/ibpy/ib_api_demo.py
Normal file
@@ -0,0 +1,90 @@
|
||||
#! /usr/bin/env python
|
||||
# -*- coding: cp1252 -*-
|
||||
# Copyright (c) 2014 Andreas
|
||||
# See LICENSE for details.
|
||||
|
||||
""" ib_api_demo """
|
||||
|
||||
from __future__ import absolute_import
|
||||
from __future__ import division
|
||||
from __future__ import print_function
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from ib.ext.Contract import Contract
|
||||
from ib.ext.Order import Order
|
||||
from ib.opt import Connection
|
||||
|
||||
|
||||
def error_handler(msg):
|
||||
"""Handles the capturing of error messages"""
|
||||
print("Server Error: %s" % msg)
|
||||
|
||||
|
||||
def reply_handler(msg):
|
||||
"""Handles of server replies"""
|
||||
print("Server Response: %s, %s" % (msg.typeName, msg))
|
||||
|
||||
|
||||
def create_contract(symbol, sec_type, exch, prim_exch, curr):
|
||||
"""Create a Contract object defining what will
|
||||
be purchased, at which exchange and in which currency.
|
||||
|
||||
symbol - The ticker symbol for the contract
|
||||
sec_type - The security type for the contract ('STK' is 'stock')
|
||||
exch - The exchange to carry out the contract on
|
||||
prim_exch - The primary exchange to carry out the contract on
|
||||
curr - The currency in which to purchase the contract"""
|
||||
contract = Contract()
|
||||
contract.m_symbol = symbol
|
||||
contract.m_secType = sec_type
|
||||
contract.m_exchange = exch
|
||||
contract.m_primaryExch = prim_exch
|
||||
contract.m_currency = curr
|
||||
return contract
|
||||
|
||||
|
||||
def create_order(order_type, quantity, action):
|
||||
"""Create an Order object (Market/Limit) to go long/short.
|
||||
|
||||
order_type - 'MKT', 'LMT' for Market or Limit orders
|
||||
quantity - Integral number of assets to order
|
||||
action - 'BUY' or 'SELL'"""
|
||||
order = Order()
|
||||
order.m_orderType = order_type
|
||||
order.m_totalQuantity = quantity
|
||||
order.m_action = action
|
||||
return order
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Connect to the Trader Workstation (TWS) running on the
|
||||
# usual port of 7496, with a clientId of 100
|
||||
# (The clientId is chosen by us and we will need
|
||||
# separate IDs for both the execution connection and
|
||||
# market data connection)
|
||||
tws_conn = Connection.create(port=4001, clientId=100)
|
||||
tws_conn.connect()
|
||||
|
||||
# Assign the error handling function defined above
|
||||
# to the TWS connection
|
||||
tws_conn.register(error_handler, 'Error')
|
||||
|
||||
# Assign all of the server reply messages to the
|
||||
# reply_handler function defined above
|
||||
tws_conn.registerAll(reply_handler)
|
||||
|
||||
# Create an order ID which is 'global' for this session. This
|
||||
# will need incrementing once new orders are submitted.
|
||||
order_id = 1
|
||||
|
||||
# Create a contract in GOOG stock via SMART order routing
|
||||
goog_contract = create_contract('GOOG', 'STK', 'SMART', 'SMART', 'USD')
|
||||
|
||||
# Go long 100 shares of Google
|
||||
goog_order = create_order('MKT', 100, 'BUY')
|
||||
|
||||
# Use the connection to the send the order to IB
|
||||
tws_conn.placeOrder(order_id, goog_contract, goog_order)
|
||||
|
||||
# Disconnect from TWS
|
||||
tws_conn.disconnect()
|
||||
302
src/ibpy/reference_python.py
Normal file
302
src/ibpy/reference_python.py
Normal file
@@ -0,0 +1,302 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
##
|
||||
# This script is an exmple of using the generated code within IbPy in
|
||||
# the same manner as the Java code. We subclass EWrapper and give an
|
||||
# instance of the wrapper to an EClientSocket.
|
||||
##
|
||||
|
||||
from sys import argv
|
||||
from time import sleep, strftime
|
||||
|
||||
from ib.ext.Contract import Contract
|
||||
from ib.ext.EWrapper import EWrapper
|
||||
from ib.ext.EClientSocket import EClientSocket
|
||||
from ib.ext.ExecutionFilter import ExecutionFilter
|
||||
|
||||
|
||||
def showmessage(message, mapping):
|
||||
try:
|
||||
del(mapping['self'])
|
||||
except (KeyError, ):
|
||||
pass
|
||||
items = list(mapping.items())
|
||||
items.sort()
|
||||
print(('### %s' % (message, )))
|
||||
for k, v in items:
|
||||
print((' %s:%s' % (k, v)))
|
||||
|
||||
|
||||
class ReferenceWrapper(EWrapper):
|
||||
def tickPrice(self, tickerId, field, price, canAutoExecute):
|
||||
showmessage('tickPrice', vars())
|
||||
|
||||
def tickSize(self, tickerId, field, size):
|
||||
showmessage('tickSize', vars())
|
||||
|
||||
def tickOptionComputation(self, tickerId, field, impliedVol, delta, optPrice, pvDividend, gamma, vega, theta, undPrice):
|
||||
showmessage('tickOptionComputation', vars())
|
||||
|
||||
def tickGeneric(self, tickerId, tickType, value):
|
||||
showmessage('tickGeneric', vars())
|
||||
|
||||
def tickString(self, tickerId, tickType, value):
|
||||
showmessage('tickString', vars())
|
||||
|
||||
def tickEFP(self, tickerId, tickType, basisPoints, formattedBasisPoints, impliedFuture, holdDays, futureExpiry, dividendImpact, dividendsToExpiry):
|
||||
showmessage('tickEFP', vars())
|
||||
|
||||
def orderStatus(self, orderId, status, filled, remaining, avgFillPrice, permId, parentId, lastFillPrice, clientId, whyHeId):
|
||||
showmessage('orderStatus', vars())
|
||||
|
||||
def openOrder(self, orderId, contract, order, state):
|
||||
showmessage('openOrder', vars())
|
||||
|
||||
def openOrderEnd(self):
|
||||
showmessage('openOrderEnd', vars())
|
||||
|
||||
def updateAccountValue(self, key, value, currency, accountName):
|
||||
showmessage('updateAccountValue', vars())
|
||||
|
||||
def updatePortfolio(self, contract, position, marketPrice, marketValue, averageCost, unrealizedPNL, realizedPNL, accountName):
|
||||
showmessage('updatePortfolio', vars())
|
||||
|
||||
def updateAccountTime(self, timeStamp):
|
||||
showmessage('updateAccountTime', vars())
|
||||
|
||||
def accountDownloadEnd(self, accountName):
|
||||
showmessage('accountDownloadEnd', vars())
|
||||
|
||||
def nextValidId(self, orderId):
|
||||
showmessage('nextValidId', vars())
|
||||
|
||||
def contractDetails(self, reqId, contractDetails):
|
||||
showmessage('contractDetails', vars())
|
||||
|
||||
def contractDetailsEnd(self, reqId):
|
||||
showmessage('contractDetailsEnd', vars())
|
||||
|
||||
def bondContractDetails(self, reqId, contractDetails):
|
||||
showmessage('bondContractDetails', vars())
|
||||
|
||||
def execDetails(self, reqId, contract, execution):
|
||||
showmessage('execDetails', vars())
|
||||
|
||||
def execDetailsEnd(self, reqId):
|
||||
showmessage('execDetailsEnd', vars())
|
||||
|
||||
def connectionClosed(self):
|
||||
showmessage('connectionClosed', {})
|
||||
|
||||
def error(self, id=None, errorCode=None, errorMsg=None):
|
||||
showmessage('error', vars())
|
||||
|
||||
def error_0(self, strvalue=None):
|
||||
showmessage('error_0', vars())
|
||||
|
||||
def error_1(self, id=None, errorCode=None, errorMsg=None):
|
||||
showmessage('error_1', vars())
|
||||
|
||||
def updateMktDepth(self, tickerId, position, operation, side, price, size):
|
||||
showmessage('updateMktDepth', vars())
|
||||
|
||||
def updateMktDepthL2(self, tickerId, position, marketMaker, operation, side, price, size):
|
||||
showmessage('updateMktDepthL2', vars())
|
||||
|
||||
def updateNewsBulletin(self, msgId, msgType, message, origExchange):
|
||||
showmessage('updateNewsBulletin', vars())
|
||||
|
||||
def managedAccounts(self, accountsList):
|
||||
showmessage('managedAccounts', vars())
|
||||
|
||||
def receiveFA(self, faDataType, xml):
|
||||
showmessage('receiveFA', vars())
|
||||
|
||||
def historicalData(self, reqId, date, open, high, low, close, volume, count, WAP, hasGaps):
|
||||
showmessage('historicalData', vars())
|
||||
|
||||
def scannerParameters(self, xml):
|
||||
showmessage('scannerParameters', vars())
|
||||
|
||||
def scannerData(self, reqId, rank, contractDetails, distance, benchmark, projection, legsStr):
|
||||
showmessage('scannerData', vars())
|
||||
|
||||
def accountDownloadEnd(self, accountName):
|
||||
showmessage('acountDownloadEnd', vars())
|
||||
|
||||
def commissionReport(self, commissionReport):
|
||||
showmessage('commissionReport', vars())
|
||||
|
||||
def contractDetailsEnd(self, reqId):
|
||||
showmessage('contractDetailsEnd', vars())
|
||||
|
||||
def currentTime(self, time):
|
||||
showmessage('currentTime', vars())
|
||||
|
||||
def deltaNeutralValidation(self, reqId, underComp):
|
||||
showmessage('deltaNeutralValidation', vars())
|
||||
|
||||
def execDetailsEnd(self, reqId):
|
||||
showmessage('execDetailsEnd', vars())
|
||||
|
||||
def fundamentalData(self, reqId, data):
|
||||
showmessage('fundamentalData', vars())
|
||||
|
||||
def marketDataType(self, reqId, marketDataType):
|
||||
showmessage('marketDataType', vars())
|
||||
|
||||
def openOrderEnd(self):
|
||||
showmessage('openOrderEnd', vars())
|
||||
|
||||
def realtimeBar(self, reqId, time, open, high, low, close, volume, wap, count):
|
||||
showmessage('realtimeBar', vars())
|
||||
|
||||
def scannerDataEnd(self, reqId):
|
||||
showmessage('scannerDataEnd', vars())
|
||||
|
||||
def tickEFP(self, tickerId, tickType, basisPoints, formattedBasisPoints, impliedFuture, holdDays, futureExpiry, dividendImpact, dividendsToExpiry):
|
||||
showmessage('tickEFP', vars())
|
||||
|
||||
def tickGeneric(self, tickerId, tickType, value):
|
||||
showmessage('tickGeneric', vars())
|
||||
|
||||
def tickSnapshotEnd(self, reqId):
|
||||
showmessage('tickSnapshotEnd', vars())
|
||||
|
||||
def error_0(self, strval):
|
||||
showmessage('error_0', vars())
|
||||
|
||||
def error_1(self, id, errorCode, errorMsg):
|
||||
showmessage('error_1', vars())
|
||||
|
||||
def position(self, account, contract, pos, avgCost):
|
||||
showmessage('position', vars())
|
||||
|
||||
def positionEnd(self):
|
||||
showmessage('positionEnd', vars())
|
||||
|
||||
def accountSummary(self, reqId, account, tag, value, currency):
|
||||
showmessage('accountSummary', vars())
|
||||
|
||||
def accountSummaryEnd(self, reqId):
|
||||
showmessage('accountSummaryEnd', vars())
|
||||
|
||||
allMethods = []
|
||||
def ref(method):
|
||||
allMethods.append(method.__name__)
|
||||
return method
|
||||
|
||||
|
||||
class ReferenceApp:
|
||||
def __init__(self, host='localhost', port=7496, clientId=0):
|
||||
self.host = host
|
||||
self.port = port
|
||||
self.clientId = clientId
|
||||
self.wrapper = ReferenceWrapper()
|
||||
self.connection = EClientSocket(self.wrapper)
|
||||
|
||||
@ref
|
||||
def eConnect(self):
|
||||
self.connection.eConnect(self.host, self.port, self.clientId)
|
||||
|
||||
@ref
|
||||
def reqAccountUpdates(self):
|
||||
self.connection.reqAccountUpdates(1, '')
|
||||
|
||||
@ref
|
||||
def reqOpenOrders(self):
|
||||
self.connection.reqOpenOrders()
|
||||
|
||||
@ref
|
||||
def reqExecutions(self):
|
||||
filt = ExecutionFilter()
|
||||
self.connection.reqExecutions(0, filt)
|
||||
|
||||
@ref
|
||||
def reqIds(self):
|
||||
self.connection.reqIds(10)
|
||||
|
||||
@ref
|
||||
def reqNewsBulletins(self):
|
||||
self.connection.reqNewsBulletins(1)
|
||||
|
||||
@ref
|
||||
def cancelNewsBulletins(self):
|
||||
self.connection.cancelNewsBulletins()
|
||||
|
||||
@ref
|
||||
def setServerLogLevel(self):
|
||||
self.connection.setServerLogLevel(3)
|
||||
|
||||
@ref
|
||||
def reqAutoOpenOrders(self):
|
||||
self.connection.reqAutoOpenOrders(1)
|
||||
|
||||
@ref
|
||||
def reqAllOpenOrders(self):
|
||||
self.connection.reqAllOpenOrders()
|
||||
|
||||
@ref
|
||||
def reqManagedAccts(self):
|
||||
self.connection.reqManagedAccts()
|
||||
|
||||
@ref
|
||||
def requestFA(self):
|
||||
self.connection.requestFA(1)
|
||||
|
||||
@ref
|
||||
def reqMktData(self):
|
||||
contract = Contract() #
|
||||
contract.m_symbol = 'AUD'
|
||||
contract.m_currency = 'USD'
|
||||
contract.m_secType = 'CASH'
|
||||
contract.m_exchange = 'IDEALPRO'
|
||||
self.connection.reqMktData(1, contract, '', False)
|
||||
|
||||
@ref
|
||||
def reqHistoricalData(self):
|
||||
contract = Contract()
|
||||
contract.m_symbol = 'QQQQ'
|
||||
contract.m_secType = 'STK'
|
||||
contract.m_exchange = 'SMART'
|
||||
endtime = strftime('%Y%m%d %H:%M:%S')
|
||||
self.connection.reqHistoricalData(
|
||||
tickerId=1,
|
||||
contract=contract,
|
||||
endDateTime=endtime,
|
||||
durationStr='1 D',
|
||||
barSizeSetting='1 min',
|
||||
whatToShow='TRADES',
|
||||
useRTH=0,
|
||||
formatDate=1)
|
||||
|
||||
@ref
|
||||
def eDisconnect(self):
|
||||
sleep(5)
|
||||
self.connection.eDisconnect()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app = ReferenceApp()
|
||||
methods = argv[1:]
|
||||
|
||||
if not methods:
|
||||
methods = ['eConnect', 'eDisconnect', ]
|
||||
elif methods == ['all']:
|
||||
methods = allMethods
|
||||
if 'eConnect' not in methods:
|
||||
methods.insert(0, 'eConnect')
|
||||
if 'eDisconnect' not in methods:
|
||||
methods.append('eDisconnect')
|
||||
|
||||
print(('### calling functions:', str.join(', ', methods)))
|
||||
for mname in methods:
|
||||
call = getattr(app, mname, None)
|
||||
if call is None:
|
||||
print(('### warning: no call %s' % (mname, )))
|
||||
else:
|
||||
print(('## calling', call.__func__.__name__))
|
||||
call()
|
||||
print(('## called', call.__func__.__name__))
|
||||
|
||||
149
src/transcode/dv2mp4.py
Normal file
149
src/transcode/dv2mp4.py
Normal file
@@ -0,0 +1,149 @@
|
||||
#! /usr/bin/env python
|
||||
# -*- coding: cp1252 -*-
|
||||
# Copyright (c) 2014 Andreas
|
||||
# See LICENSE for details.
|
||||
|
||||
""" compress DV tape video files to mp4 """
|
||||
|
||||
from __future__ import absolute_import
|
||||
from __future__ import division
|
||||
from __future__ import print_function
|
||||
from __future__ import unicode_literals
|
||||
from future.builtins.disabled import *
|
||||
|
||||
from argparse import ArgumentParser
|
||||
import os
|
||||
import shlex
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
import re
|
||||
import datetime as dt
|
||||
|
||||
|
||||
def transcode(argv=None):
|
||||
if argv is None:
|
||||
argv = sys.argv[1:]
|
||||
# parse options and arguments
|
||||
parser = ArgumentParser(description="handbrake")
|
||||
parser.add_argument("-d")
|
||||
parser.add_argument("-o")
|
||||
args = parser.parse_args(argv)
|
||||
print("starting '{} {}'".format(os.path.basename(argv[0]), args))
|
||||
handbrake = r'C:\Program Files\Handbrake\HandbrakeCLI.exe'
|
||||
# startup application
|
||||
files = os.listdir(args.d)
|
||||
for file in sorted(files):
|
||||
root, ext = os.path.splitext(file)
|
||||
if ext != ".avi":
|
||||
continue
|
||||
fn = os.path.join(args.d, file)
|
||||
unix_mod_mtime = os.stat(fn).st_mtime
|
||||
mtime = dt.datetime.fromtimestamp(unix_mod_mtime)
|
||||
mp4_fn = "%s-DV.mp4" % mtime.strftime("%Y%m%d-%H%M%S")
|
||||
mp4_fp = os.path.join(args.o, mp4_fn)
|
||||
hbargs = [handbrake, '-i', fn, '-o', mp4_fp]
|
||||
opts = '-t 1 --angle 1 -c 1 -f mp4 --decomb --strict-anamorphic --keep-display-aspect --optimize -e x264 -b 1350 -2 -T --cfr -a 1 -E faac -6 stereo -R Auto -B 128 -D 0 --gain 0 --audio-fallback ffac3 --x264-tune=film --verbose=1'.split()
|
||||
hbargs += opts
|
||||
print(" ".join((shlex.quote(a) for a in hbargs)))
|
||||
subprocess.call(hbargs)
|
||||
shutil.copystat(fn, mp4_fp)
|
||||
print("done.")
|
||||
|
||||
|
||||
REDV = r'(?P<year>\d{4})-(?P<mon>\d{2})-(?P<day>\d{2}) (?P<hh>\d{2})\.(?P<mm>\d{2})\.(?P<ss>\d{2})'
|
||||
|
||||
|
||||
def rename_dv(argv=None):
|
||||
# 2002-07-28 02.22.56 Werne.mp4
|
||||
# DV, SD, HD
|
||||
if argv is None:
|
||||
argv = sys.argv[1:]
|
||||
parser = ArgumentParser(description="batch rename")
|
||||
parser.add_argument("-d")
|
||||
args = parser.parse_args(argv)
|
||||
print("starting '{} {}'".format(os.path.basename(argv[0]), args))
|
||||
reo = re.compile(REDV)
|
||||
files = os.listdir(args.d)
|
||||
for file in sorted(files):
|
||||
root, ext = os.path.splitext(file)
|
||||
mo = reo.match(root)
|
||||
if not mo:
|
||||
print(file)
|
||||
continue
|
||||
day = mo.group('day')
|
||||
mon = mo.group('mon')
|
||||
year = mo.group('year')
|
||||
hh = mo.group('hh')
|
||||
mm = mo.group('mm')
|
||||
ss = mo.group('ss')
|
||||
fn2 = "{}{}{}-{}{}{}-DV{}".format(year, mon, day, hh, mm, ss, ext)
|
||||
print("{} -> {}".format(file, fn2))
|
||||
# os.rename(os.path.join(args.d, file), os.path.join(args.d, fn2))
|
||||
|
||||
RESD = r'MOV-(?P<year>\d{4})(?P<mon>\d{2})(?P<day>\d{2})-(?P<hh>\d{2})(?P<mm>\d{2})(?P<ss>\d{2})\.MP4'
|
||||
|
||||
|
||||
def rename_sd(argv=None):
|
||||
# MOV-20081124-095630.MP4
|
||||
# 20020728-022256-SD.mp4
|
||||
if argv is None:
|
||||
argv = sys.argv[1:]
|
||||
parser = ArgumentParser(description="batch rename")
|
||||
parser.add_argument("-d")
|
||||
args = parser.parse_args(argv)
|
||||
print("starting '{} {}'".format(os.path.basename(argv[0]), args))
|
||||
reo = re.compile(RESD)
|
||||
dirs = os.walk(args.d)
|
||||
for dir_, _, files in dirs:
|
||||
for file in files:
|
||||
mo = reo.match(file)
|
||||
if not mo:
|
||||
print(file)
|
||||
continue
|
||||
day = mo.group('day')
|
||||
mon = mo.group('mon')
|
||||
year = mo.group('year')
|
||||
hh = mo.group('hh')
|
||||
mm = mo.group('mm')
|
||||
ss = mo.group('ss')
|
||||
fn2 = "{}{}{}-{}{}{}-SD.mp4".format(year, mon, day, hh, mm, ss)
|
||||
print("{}:{} -> {}".format(dir_, file, fn2))
|
||||
# os.rename(os.path.join(dir_, file), os.path.join(dir_, fn2))
|
||||
|
||||
|
||||
REHD = r'(?P<year>\d{4})-(?P<mon>\d{2})-(?P<day>\d{2})_(?P<hh>\d{2})(?P<mm>\d{2})(?P<ss>\d{2})\.mp4'
|
||||
|
||||
|
||||
def rename_hd(argv=None):
|
||||
# 2012-08-02_154511.mp4
|
||||
# 20020728-022256-HD.mp4
|
||||
if argv is None:
|
||||
argv = sys.argv[1:]
|
||||
parser = ArgumentParser(description="batch rename")
|
||||
parser.add_argument("-d")
|
||||
args = parser.parse_args(argv)
|
||||
print("starting '{} {}'".format(os.path.basename(argv[0]), args))
|
||||
reo = re.compile(REHD)
|
||||
dirs = os.walk(args.d)
|
||||
for dir_, _, files in dirs:
|
||||
for file in files:
|
||||
mo = reo.match(file)
|
||||
if not mo:
|
||||
print(file)
|
||||
continue
|
||||
day = mo.group('day')
|
||||
mon = mo.group('mon')
|
||||
year = mo.group('year')
|
||||
hh = mo.group('hh')
|
||||
mm = mo.group('mm')
|
||||
ss = mo.group('ss')
|
||||
fn2 = "{}{}{}-{}{}{}-HD.mp4".format(year, mon, day, hh, mm, ss)
|
||||
print("{}:{} -> {}".format(dir_, file, fn2))
|
||||
# os.rename(os.path.join(dir_, file), os.path.join(dir_, fn2))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
transcode(["-d", r"E:\Shared Documents\Raw\Sony TRV-25E\DV", "-o", r"D:\Videos"])
|
||||
# transcode(["-f", r"D:\Users\Public\Videos\Balogh\2008\Seehofpark\20081124-095630-SD.mp4"])
|
||||
# rename_hd(["-d", r"D:\Users\Public\Videos\Balogh"])
|
||||
86
src/transcode/mts2mp4.py
Normal file
86
src/transcode/mts2mp4.py
Normal file
@@ -0,0 +1,86 @@
|
||||
#! /usr/bin/env python3
|
||||
# -*- coding: cp1252 -*-
|
||||
# Copyright (c) 2014 Andreas
|
||||
# See LICENSE for details.
|
||||
|
||||
""" compress Panasonic HDR mts video files to mp4 """
|
||||
|
||||
from __future__ import absolute_import
|
||||
from __future__ import division
|
||||
from __future__ import print_function
|
||||
from __future__ import unicode_literals
|
||||
from future.builtins.disabled import *
|
||||
|
||||
from argparse import ArgumentParser
|
||||
import os
|
||||
import re
|
||||
import shlex
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
import datetime as dt
|
||||
|
||||
|
||||
def transcode(argv=None):
|
||||
if argv is None:
|
||||
argv = sys.argv[1:]
|
||||
# parse options and arguments
|
||||
parser = ArgumentParser(description="handbrake")
|
||||
parser.add_argument("-d")
|
||||
parser.add_argument("-o")
|
||||
args = parser.parse_args(argv)
|
||||
print("starting '{} {}'".format(os.path.basename(argv[0]), args))
|
||||
handbrake = r'C:\Program Files\Handbrake\HandbrakeCLI.exe'
|
||||
# startup application
|
||||
for dir_, _, files in os.walk(args.d):
|
||||
for file in files:
|
||||
root, ext = os.path.splitext(file)
|
||||
if ext not in (".m2ts", '.mts', '.MTS'):
|
||||
continue
|
||||
fn = os.path.join(dir_, file)
|
||||
unix_mod_mtime = os.stat(fn).st_mtime
|
||||
mtime = dt.datetime.fromtimestamp(unix_mod_mtime)
|
||||
mp4_fn = "%s-HD.mp4" % mtime.strftime("%Y%m%d-%H%M%S")
|
||||
mp4_fp = os.path.join(args.o, mp4_fn)
|
||||
hbargs = [handbrake, '-i', fn, '-o', mp4_fp]
|
||||
opts = '-t 1 --angle 1 -c 1 -f mp4 --decomb --strict-anamorphic --keep-display-aspect --optimize -e x264 -b 6700 -2 -T --cfr -a 1 -E faac -6 stereo -R Auto -B 160 -D 0 --gain 0 --audio-fallback ffac3 --x264-tune=film --verbose=1'.split()
|
||||
hbargs += opts
|
||||
print(" ".join((shlex.quote(a) for a in hbargs)))
|
||||
subprocess.call(hbargs)
|
||||
shutil.copystat(fn, mp4_fp)
|
||||
print("done.")
|
||||
|
||||
|
||||
REMTS = r'(?P<no>\d{5})\.MTS'
|
||||
|
||||
|
||||
def rename_mts(argv=None):
|
||||
# 2002-07-28 02.22.56 Werne.mp4
|
||||
# DV, SD, HD
|
||||
if argv is None:
|
||||
argv = sys.argv[1:]
|
||||
parser = ArgumentParser(description="batch rename")
|
||||
parser.add_argument("-d")
|
||||
args = parser.parse_args(argv)
|
||||
print("starting '{} {}'".format(os.path.basename(argv[0]), args))
|
||||
reo = re.compile(REMTS)
|
||||
files = os.listdir(args.d)
|
||||
for fn in sorted(files):
|
||||
file = os.path.join(args.d, fn)
|
||||
root, ext = os.path.splitext(fn)
|
||||
mo = reo.match(fn)
|
||||
if not mo:
|
||||
print(file)
|
||||
continue
|
||||
st = os.stat(file)
|
||||
mt = dt.datetime.fromtimestamp(st.st_mtime)
|
||||
fn2 = "{:04}{:02}{:02}-{:02}{:02}{:02}-HD{}".format(mt.year, mt.month, mt.day, mt.hour, mt.minute, mt.second, ext)
|
||||
dest = os.path.join(args.d, fn2)
|
||||
print("{} -> {}".format(file, fn2))
|
||||
os.rename(file, dest)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
transcode(["-d", r"E:\Shared Documents\Raw\Panasonic HDC-SD99", "-o", r"D:\Videos"])
|
||||
# rename_mts(["-d", r"E:\Shared Documents\Raw\Panasonic HDC-SD99\20140720"])
|
||||
@@ -1,3 +1,6 @@
|
||||
#! /usr/bin/env python2
|
||||
# -*- coding: cp1252 -*-
|
||||
|
||||
# ZODB Tutorial
|
||||
|
||||
""" zodb.tutorial """
|
||||
|
||||
Reference in New Issue
Block a user