Commit fb87518e authored by Lysander Trischler's avatar Lysander Trischler

Manage index on isobeef

parent 16a0b6c0
#!/usr/bin/python
#!/usr/bin/python3
# encoding: utf-8
#
# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
......@@ -27,41 +27,39 @@ __copyright__ = "Copyright 2012, Lysander Trischler"
__license__ = "WTFPL"
__maintainer__ = "Lysander Trischler"
__email__ = "software@lyse.isobeef.org"
__version__ = "0.2"
__version_info__ = (0, 2)
__version__ = "0.3"
__version_info__ = (0, 3)
import os.path
import os
import time
import ConfigParser
import urlparse
import urllib2
import configparser
import urllib.parse
import urllib.request
class Config(ConfigParser.ConfigParser):
class Config(configparser.ConfigParser):
def __init__(self, fp):
ConfigParser.ConfigParser.__init__(self)
configparser.ConfigParser.__init__(self)
self.read([fp])
def get(self, section, field, default):
try:
return ConfigParser.ConfigParser.get(self, section, field)
except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
return configparser.ConfigParser.get(self, section, field)
except (configparser.NoSectionError, configparser.NoOptionError):
return default
def path(*path):
return os.path.join(*[os.path.expanduser(os.path.expandvars(segment)) for segment in path])
DATA_DIR = path(os.getenv("XDG_DATA_HOME", "~/.local/share"), "yfav")
CONFIG_FILE = path(os.getenv("XDG_CONFIG_HOME", "~/.config"), "yfav") # never used
CONFIG_FILE = path(os.getenv("XDG_CONFIG_HOME", "~/.config"), "yfav")
INDEX_FILE = path(DATA_DIR, "index")
ATOM_FILE = path(DATA_DIR, "atom")
CONFIG = Config(CONFIG_FILE)
ATOM_FILE = path(CONFIG.get("atom", "path", "~/srv/yfav.atom"))
VIDEO_ID_LEN = 11
VIDEO_ID_URL = "http://youtube.com/watch?v="
CONFIG = Config(CONFIG_FILE)
def now():
return time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime())
......@@ -71,7 +69,7 @@ _XHTML_ESCAPE_RE = re.compile('[&<>"]')
_XHTML_ESCAPE_DICT = {'&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;'}
def esc(value):
"""Escapes a string so it is valid within XML or XHTML."""
if value is None: return u''
if value is None: return ''
return _XHTML_ESCAPE_RE.sub(lambda match: _XHTML_ESCAPE_DICT[match.group(0)], value)
class Video(object):
......@@ -102,8 +100,8 @@ class Video(object):
@classmethod
def get_info(cls, video_id):
info = urllib2.urlopen('http://youtube.com/get_video_info?video_id=%s' % video_id)
data = urlparse.parse_qs(info.read())
info = urllib.request.urlopen('http://youtube.com/get_video_info?video_id=%s' % video_id)
data = urllib.parse.parse_qs(str(info.read()))
info.close()
return data
......@@ -171,29 +169,42 @@ def gen_atom(max=None):
'<link rel="alternate" href="' + esc(video.url) + '" /></entry>\n')
atom.write('</feed>\n')
def upload():
os.system("scp -q " + ATOM_FILE
+ " " + CONFIG.get("scp", "user", "lyse")
+ "@" + CONFIG.get("scp", "host", "isobeef.org")
+ ":" + CONFIG.get("scp", "path", "srv/yfav.atom"))
def upload(video_id):
cmd = "ssh " + CONFIG.get("ssh", "user", "lyse") \
+ "@" + CONFIG.get("ssh", "host", "isobeef.org") \
+ " '" + CONFIG.get("ssh", "yfav", "~/bin/yfav") \
+ " " + video_id + "'"
e = os.system(cmd) % 255
if e != 0:
raise ValueError("%s\nexit code %d" % (cmd, e))
if __name__ == "__main__":
import sys
import socket
hostname = os.getenv("HOST") or os.getenv("HOSTNMAE") or socket.gethostname()
if hostname == "isobeef.org":
process = add_fav
else:
process = upload
def nop(ignore=None):
pass
gen_atom = nop
if len(sys.argv) > 1:
for video_id in sys.argv[1:]:
add_fav(video_id)
process(video_id)
else:
if sys.stdin.isatty():
while True:
try:
video_id = raw_input("YouTube Video ID > ")
video_id = input("YouTube Video ID > ")
if video_id:
add_fav(video_id)
process(video_id)
except EOFError:
break
else:
for video_id in sys.stdin.readlines():
add_fav(video_id.strip())
process(video_id.strip())
gen_atom()
upload()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment