浏览代码

added station list

Gamaliel Espinoza 8 年之前
父节点
当前提交
4c5896d535
共有 2 个文件被更改,包括 86 次插入74 次删除
  1. 1 1
      tools/fourier/__init__.py
  2. 85 73
      tools/fourier/cli.py

+ 1 - 1
tools/fourier/__init__.py

@@ -1 +1 @@
-__version__ = '1.0.0b6'
+__version__ = '1.0.1'

+ 85 - 73
tools/fourier/cli.py

@@ -55,6 +55,9 @@ def main():
                           default=False
                           )
 
+    stations_parser = subparsers.add_parser('station')
+    stations_parser.add_argument('action')
+
     args = parser.parse_args()
     action = args.action
 
@@ -64,81 +67,90 @@ def main():
     device_id = config['device_id']
     dbpath = '/var/fourier/{}/files.db'.format(device_id)
 
-    if action == 'stats':
-        conn = sqlite3.connect(dbpath)
-        cursor = conn.cursor()
-        cursor.execute("select count(*), count(uploaded) from file")
-        total, uploaded, = cursor.fetchone()
-        print("total: {}".format(total))
-        print("uploaded: {}".format(uploaded))
-        print("pending: {}".format(total - uploaded))
-
-    elif action == 'index-files':
-        counter = 0
-        already_indexed = 0
-        conn = sqlite3.connect(dbpath)
-        path = os.path.join('/var/fourier', device_id)
-        if args.station:
-            path = os.path.join(path, args.station)
-        for folder, folders, files in os.walk(path):
-            for file in files:
-                if not file.endswith('.mp3'):
-                    continue
-
-                filename = os.path.join(folder, file)
-
-                dt = datetime.strptime(
-                    file[:19],
-                    '%Y-%m-%dT%H-%M-%S'
-                )
-                try:
-                    if args.from_date:
-                        do_insert = dt >= args.from_date  
-                    else:
-                        do_insert = True
-
-                    if do_insert:
-                        thehash = hash_file(filename)
-                        insert_file(thehash, filename, dt, db=conn)
-                        counter += 1
-                        print(dt)
-
-                except sqlite3.IntegrityError:
-                    already_indexed += 1
-                    print('already indexed: {}'.format(filename))
-
-        if not args.dry_run:
-            conn.commit()
-        else:
-            conn.rollback()
-            print('\n[WARNING] DRY RUN FINISHED')
-
-        print('----------------------------------')
-        print('total files indexed: {}'.format(counter))
-        print('total files in existence: {}'.format(already_indexed))
-        print('----------------------------------')
-
-    elif action == 'setup':
-        if not os.path.isfile(dbpath):
+    if args.entity == 'db':
+        if action == 'stats':
             conn = sqlite3.connect(dbpath)
             cursor = conn.cursor()
-            sentences = [
-                """create table file(
-                    hash text primary key,
-                    station text,
-                    timestamp int,
-                    filename text,
-                    uploaded int
-                )""",
-                "create index timestamp_index_desc on file (timestamp desc)",
-                "create index timestamp_index_asc on file (timestamp desc)",
-            ]
-            for query in sentences:
-                cursor.execute(query)
-            conn.commit()
-        else:
-            print('database already installed')
-            sys.exit(1)
+            cursor.execute("select count(*), count(uploaded) from file")
+            total, uploaded, = cursor.fetchone()
+            print("total: {}".format(total))
+            print("uploaded: {}".format(uploaded))
+            print("pending: {}".format(total - uploaded))
+
+        elif action == 'index-files':
+            counter = 0
+            already_indexed = 0
+            conn = sqlite3.connect(dbpath)
+            path = os.path.join('/var/fourier', device_id)
+            if args.station:
+                path = os.path.join(path, args.station)
+            for folder, folders, files in os.walk(path):
+                for file in files:
+                    if not file.endswith('.mp3'):
+                        continue
+
+                    filename = os.path.join(folder, file)
+
+                    dt = datetime.strptime(
+                        file[:19],
+                        '%Y-%m-%dT%H-%M-%S'
+                    )
+                    try:
+                        if args.from_date:
+                            do_insert = dt >= args.from_date  
+                        else:
+                            do_insert = True
+
+                        if do_insert:
+                            thehash = hash_file(filename)
+                            insert_file(thehash, filename, dt, db=conn)
+                            counter += 1
+                            print(dt)
+
+                    except sqlite3.IntegrityError:
+                        already_indexed += 1
+                        print('already indexed: {}'.format(filename))
+
+            if not args.dry_run:
+                conn.commit()
+            else:
+                conn.rollback()
+                print('\n[WARNING] DRY RUN FINISHED')
+
+            print('----------------------------------')
+            print('total files indexed: {}'.format(counter))
+            print('total files in existence: {}'.format(already_indexed))
+            print('----------------------------------')
+
+        elif action == 'setup':
+            if not os.path.isfile(dbpath):
+                conn = sqlite3.connect(dbpath)
+                cursor = conn.cursor()
+                sentences = [
+                    """create table file(
+                        hash text primary key,
+                        station text,
+                        timestamp int,
+                        filename text,
+                        uploaded int
+                    )""",
+                    "create index timestamp_index_desc on file (timestamp desc)",
+                    "create index timestamp_index_asc on file (timestamp desc)",
+                ]
+                for query in sentences:
+                    cursor.execute(query)
+                conn.commit()
+            else:
+                print('database already installed')
+                sys.exit(1)
+
+    elif args.entity == 'station':
+        if action == 'list':
+            stations_path = os.path.join('/var/fourier', device_id)
+            dirs = os.listdir(stations_path)
+            for dr in dirs:
+                if '.' not in dr:
+                    print(dr)
                         
 
 if __name__ == '__main__':