import re
import ast
import pandas
import pickle
import numpy as np
import glob
import heapq
import operator
from apk import APK
import matplotlib.pyplot as plt
import os
import commands
import json
import pickle
import pprint
import pandas as pd
import numpy
from numpy.random import randint

with open('features_list.txt', 'r') as f:
    features_list = ast.literal_eval(f.read())   # features_list is list of 456 features obtained from previous analysis and stored in txt file
count=0
for filepath in glob.iglob("/home/aman/tsting/*"):
    df = pd.DataFrame(columns=features_list)
    current=[]    # list for this particular app's all features
    try:          # try is necessary as some apps through error on calling below functions and remaining apps are not analyzed
        apkf = APK(filepath)
        current=current+apkf.get_activities()+apkf.get_services()+apkf.get_receivers()+apkf.get_providers()+apkf.get_permissions()
    except:
        continue
    dict={}
    current= list(set(current))
    for i in features_list:
        dict[i]=0
    for i in current:
        if i in dict:
            dict[i]+=1     # this increments the count of a particular feature in an app
    for i in features_list:
        df.at[count,i]=dict[i]

    count+=1               # count keeps the number of apps analyzed and name of csv of each app is after its count
    csv_filename = str(count);
    df.to_csv(csv_filename+".csv", encoding='utf-8', index=False)  # makes csv of 1 row and 456 columns for each app in the folder
