living-lab-admin added
authorP2P-Next <p2p@p2p-next-02.grid.pub.ro>
Mon, 21 Mar 2011 11:54:18 +0000 (13:54 +0200)
committerP2P-Next <p2p@p2p-next-02.grid.pub.ro>
Mon, 21 Mar 2011 11:54:18 +0000 (13:54 +0200)
trial-site/living-lab-admin/config_thumbnails.sh [new file with mode: 0755]
trial-site/living-lab-admin/gen_videos_info.sh [new file with mode: 0755]
trial-site/living-lab-admin/publish_videos.py [new file with mode: 0644]

diff --git a/trial-site/living-lab-admin/config_thumbnails.sh b/trial-site/living-lab-admin/config_thumbnails.sh
new file mode 100755 (executable)
index 0000000..6e8b624
--- /dev/null
@@ -0,0 +1,26 @@
+#!/bin/bash
+#
+# Used by the Python script publish_videos.py
+#
+# Author: Calin-Andrei Burloiu, 2011, calin.burloiu@gmail.com
+#
+
+if [ $# -ne 2 ]; then
+    echo "usage: $0 basename content_alias"
+    exit 1
+fi
+
+basename="$1"
+alias="$2"
+
+dir1="/home/projects/p2p-next/public_html/uploads/images/catalog_src"
+dir2="/home/projects/p2p-next/public_html/uploads/images/catalog"
+
+cp "${basename}_big.jpg" "${dir1}/${alias}_src_1.jpg"
+chmod a+w "${dir1}/${alias}_src_1.jpg"
+cp "${basename}_big.jpg" "${dir2}/${alias}_f_1_400_1.jpg"
+chmod a+w "${dir2}/${alias}_f_1_400_1.jpg"
+cp "${basename}_big.jpg" "${dir2}/${alias}_s_1_110_1.jpg"
+chmod a+w "${dir2}/${alias}_s_1_110_1.jpg"
+cp "${basename}_small.jpg" "${dir2}/${alias}_t_1_90_1.jpg"
+chmod a+w "${dir2}/${alias}_t_1_90_1.jpg"
diff --git a/trial-site/living-lab-admin/gen_videos_info.sh b/trial-site/living-lab-admin/gen_videos_info.sh
new file mode 100755 (executable)
index 0000000..a17cb2b
--- /dev/null
@@ -0,0 +1,25 @@
+#!/bin/bash
+#
+# Generates a video.info file. It needs the path from the file system where
+# the torrent files are located and the path accesible from the url of 
+# those files.
+#
+# Author: Calin-Andrei Burloiu, 2011, calin.burloiu@gmail.com
+#
+
+if [ $# -ne 2 ]; then
+    echo "usage: $0 path web_path"
+    exit 1
+fi
+
+path="$1"
+web_path="$2"
+
+output="${path}/videos.info"
+
+tstream_array=(${path}/*.tstream)
+for f in ${tstream_array[@]}; do
+    tstream="$(basename $f)"
+    echo "${web_path}/${tstream}" >> $output
+    echo "    " >> $output
+done
diff --git a/trial-site/living-lab-admin/publish_videos.py b/trial-site/living-lab-admin/publish_videos.py
new file mode 100644 (file)
index 0000000..8a35dfd
--- /dev/null
@@ -0,0 +1,233 @@
+import sys
+import urllib
+import MySQLdb
+import os
+import string
+
+
+# cms_content table
+class Content:
+       tableName = "cms_content"
+       seqTableName = "cms_content_seq"
+       user = 3
+
+       def __init__(self, dbCur, content_id, title, category):
+               self.category = category
+
+               # Columns:
+               self.dbCur = dbCur
+               self.content_id = content_id
+               self.content_name = title
+               self.type = 'catalogitem'
+               self.owner_id = Content.user
+               self.parent_id = self.getParentId()
+               self.template_id = 17
+               self.item_order = self.getItemOrder()
+               self.hierarchy = Content.getHierarchy(self.category, self.item_order)
+               self.default_content = 0
+               self.menu_text = title
+               self.content_alias = Content.encodeAlias(title)
+               self.show_in_menu = 1
+               self.collapsed = 0
+               self.markup = 'html'
+               self.active = 1
+               self.cachable = 0
+               otherHierarchies = self.getOtherHierarchies()
+               self.id_hierarchy = otherHierarchies['id_hierarchy']
+               self.hierarchy_path = otherHierarchies['hierarchy_path']
+               self.prop_names = 'Torrent,target,image,thumbnail,extra1,extra2,extra3,Description,sub_template'
+               self.metadata = '<!-- Add code here that should appear in the metadata section of all new pages -->'
+               self.titleattribute = ''
+               self.tabindex = ''
+               self.accesskey = ''
+               self.last_modified_by = Content.user
+               self.create_date = 'NOW()'
+               self.modified_date = 'NOW()'
+               self.secure = 0
+               self.page_url = ''
+       
+       def getParentId(self):
+               query = "SELECT content_id FROM `" + Content.tableName + "` WHERE hierarchy = '" + Content.getHierarchy(self.category, None) + "'"
+               self.dbCur.execute(query)
+               row = self.dbCur.fetchone()
+               
+               return int(row[0])
+
+       def getItemOrder(self):
+               query = "SELECT MAX(item_order) FROM `" + Content.tableName + "` WHERE parent_id = " + str(self.parent_id)
+               self.dbCur.execute(query)
+               row = self.dbCur.fetchone()
+               if row[0] == None:
+                       item_order = 1
+               else:
+                       item_order = row[0] + 1
+
+               return item_order
+
+       # Returns the id_hierarchy and hierarchy_path.
+       def getOtherHierarchies(self):
+               query = "SELECT content_id, content_alias from `" + Content.tableName + "` WHERE hierarchy = '%(h)s'"
+               
+               tokens = self.category.split('.')
+               
+               hierarchy = ''
+               id_hierarchy = ''
+               hierarchy_path = ''
+               for token in tokens:
+                       hierarchy += ('%05d' % int(token)) + '.'
+                       self.dbCur.execute(query % {'h': hierarchy[0:-1]})
+                       row = self.dbCur.fetchone()
+                       id_hierarchy += '' + str(row[0]) + '.'
+                       hierarchy_path += row[1] + '/'
+               
+               id_hierarchy += str(self.content_id)
+               hierarchy_path += self.content_alias
+               return {'id_hierarchy': id_hierarchy, 'hierarchy_path': hierarchy_path}
+
+       def insert(self):
+               query = "INSERT INTO `" + Content.tableName + "` (content_id, content_name, type, owner_id, parent_id, template_id, item_order, hierarchy, default_content, menu_text, content_alias, show_in_menu, collapsed, markup, active, cachable, id_hierarchy, hierarchy_path, prop_names, metadata, titleattribute, tabindex, accesskey, last_modified_by, create_date, modified_date, secure, page_url) VALUES (" + str(self.content_id) + ", '" + self.content_name + "', '" + self.type + "', " + str(self.owner_id) + ", " + str(self.parent_id) + ", " + str(self.template_id) + ", " + str(self.item_order) + ", '" + self.hierarchy + "', " + str(self.default_content) + ", '" + self.menu_text + "', '" + self.content_alias + "', " + str(self.show_in_menu) + ", " + str(self.collapsed) + ", '" + self.markup + "', " + str(self.active) + ", " + str(self.cachable) + ", '" + self.id_hierarchy + "', '" + self.hierarchy_path + "', '" + self.prop_names + "', '" + self.metadata + "', '" + self.titleattribute + "', '" + self.tabindex + "', '" + self.accesskey + "', " + str(self.last_modified_by) + ", " + self.create_date + ", " + self.modified_date + ", " + str(self.secure) + ", '" + self.page_url + "')"
+               self.dbCur.execute(query)       
+       
+       @staticmethod
+       def encodeAlias(str):
+               table = string.maketrans(' ~`!@#$%^&*()=+[{]};:\'"\|,<.>/?', '_------------------------------')
+               return urllib.quote(str.translate(table), '') + "_auto"
+       
+       # Transform category argument into hierarchy column representation from 
+       # cms_content table (ex.: 1.2.3 becomes 00001.00002.00003)
+       @staticmethod
+       def getHierarchy(category, itemOrder = None):
+               tokens = category.split('.')
+               
+               hierarchy = ''
+               for token in tokens:
+                       hierarchy += ('%05d' % int(token)) + '.'
+               
+               if itemOrder == None:
+                       return hierarchy[0:-1]
+               else:
+                       return hierarchy + ('%05d' % int(itemOrder))
+
+       @staticmethod
+       def getContentId(dbCur, category):
+               #query = "SELECT MAX(content_id) FROM `" + Content.tableName + "` WHERE parent_id = (SELECT content_id FROM `" + Content.tableName + "` WHERE hierarchy = '" + Content.getHierarchy(category, None) + "')"
+               query = "SELECT id FROM `" + Content.seqTableName + "`"
+               dbCur.execute(query)
+               row = dbCur.fetchone()
+               
+               return int(row[0]) + 1
+               #return int(row[0])
+
+       @staticmethod
+       def setContentId(dbCur, seq):
+               query = "UPDATE `" + Content.seqTableName + "` SET id = " + str(seq)
+               dbCur.execute(query)
+       
+       @staticmethod
+       def getAllContentAliases(dbCur):
+               content_aliases = set()
+               query = "SELECT content_alias FROM `" + Content.tableName + "`"
+               dbCur.execute(query)
+
+               while(True):
+                       row = dbCur.fetchone()
+                       if row == None:
+                               break
+                       content_aliases.add(row[0])
+
+               return content_aliases
+
+
+# cms_content_props table
+class ContentProps:
+       tableName = "cms_content_props" 
+
+       def __init__(self, dbCur, content_id, prop_name, torrent):
+               self.dbCur = dbCur
+
+               self.content_id = content_id
+               self.type = 'string'
+               self.prop_name = prop_name
+               self.param1 = ''
+               self.param2 = ''
+               self.param3 = ''
+               self.content = {'Torrent': torrent, 'image': '-1', 'thumbnail': '-1',
+                               'sub_template': '7', 'target': '', 'extra1': '',
+                               'extra2': '', 'extra3': '',
+                               'Description': ''}[prop_name]
+               self.create_date = 'NOW()'
+               self.modified_date = 'NOW()'
+       
+       def insert(self):
+               query = "INSERT INTO `" + ContentProps.tableName + "` (content_id, type, prop_name, param1, param2, param3, content, create_date, modified_date) VALUES (" + str(self.content_id) + ", '" + self.type + "', '" + self.prop_name + "', '" + self.param1 + "', '" + self.param2 + "', '" + self.param3 + "', '" + self.content + "', " + self.create_date + ", " + self.modified_date + ")"
+               self.dbCur.execute(query)       
+
+
+def main():
+       # Check arguments.
+       if len(sys.argv) < 3:
+               sys.stdout.write('usage: ' + sys.argv[0] + ' videos_info_file category [thumbnails_path]\n')
+               exit(1)
+
+       # Command line arguments
+       fileName = sys.argv[1]
+       category = sys.argv[2]
+        if len(sys.argv) == 4:
+            thumbsDir = sys.argv[3]
+       else:
+            thumbsDir = None
+
+       propNames = ['Torrent', 'image', 'thumbnail',
+                               'sub_template', 'target', 'extra1',
+                               'extra2', 'extra3',
+                               'Description']
+
+       # Connect to DB
+       dbConn = MySQLdb.connect(host = 'koala.cs.pub.ro', user = 'koala_p2pnext',
+                       passwd = 'ahmitairoo', db = 'koala_p2pnext')
+       dbCur = dbConn.cursor()
+
+       # Get a set of all content aliases.
+       content_aliases = Content.getAllContentAliases(dbCur)
+
+       # Open info file with torrents and titles.
+       file = open(fileName, 'r')
+
+       # Read torrents and titles file.
+       torrent = file.readline()
+       while torrent != '':
+               torrent = torrent.strip()
+               title = file.readline().strip()
+               
+               if not Content.encodeAlias(title) in content_aliases:
+                       print '*'
+                       # Database operations:
+                       contentId = Content.getContentId(dbCur, category)
+                       # Add entry in cms_content table
+                       content = Content(dbCur, contentId, title, category)
+                       content.insert()
+                       # Add entries in cms_content_props table
+                       for propName in propNames:
+                               contentProps = ContentProps(dbCur, contentId, propName, torrent)
+                               contentProps.insert()
+                       # Update content_id sequence in cms_content_seq table
+                       Content.setContentId(dbCur, contentId)
+                       
+                       if thumbsDir != None:
+                               # Configure thumbnail images
+                               alias = content.content_alias
+                               basename = thumbsDir + "/" + torrent.rpartition('/')[2].rpartition('_')[0]
+                               ##print 'basename: ' + basename + '; alias: ' + alias
+                               os.system("./config_thumbnails.sh " + basename + " " + alias)
+
+               torrent = file.readline()
+
+       # Clean-up
+       dbCur.close()
+       dbConn.close()
+
+       return 0
+
+
+if __name__ == "__main__":
+       sys.exit(main())