--- /dev/null
+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())