from arcgis.gis import GIS
from getpass import getpass
portal_url = "https://<portalhost>/<webadaptor>" #e.g. "https://myorg.maps.arcgis.com" or "https://myportal.domain.com.portal"
username = "<username>" #Provide an Admin user (built-in)
pw = getpass()
gis = GIS(portal_url, username, pw)

#Search for all Web Mapping Applications in an organization
apps = gis.content.search(query="",item_type="Web Mapping Application", max_items=10000)

#Define the list of deprecated Configurable Web Apps and other templates
deprecated_portal_apps = [
    'apps/3DViz/index.html',
    'apps/AttachmentViewer/index.html',
    'apps/FilterGallery/index.html ',
    'apps/CompareAnalysis/index.html',
    'apps/Compare3d/index.html',
    'apps/Directions/index.html',
    'apps/Editor/index.html ',
    'apps/Profile/index.html',
    'apps/InteractiveFilter/index.html',
    'apps/ImageInterpretation/index.html ',
    'apps/ImageryViewer/index.html ',
    'apps/ImpactSummary/index.html ',
    'apps/InformationLookup/index.html ',
    'apps/InteractiveLegend/index.html',
    'apps/LayerShowcase/index.html',
    'apps/LocalPerspective/index.html ',
    'apps/MapCarousel/index.html ',
    'apps/Styler/index.html',
    'apps/MapTools/index.html ',
    'apps/MapAndAppGallery/index.html ',
    'apps/Media/index.html',
    'apps/MinimalGallery/index.html',
    'apps/Minimalist/index.html',
    'apps/LocalPerspective/index.html ',
    'apps/PublicGallery/index.html ',
    'apps/PublicInformation/index.html ',
    'apps/Styler/index.html',
    'apps/3DInsetMap/index.html ',
    'apps/SimpleViewer/index.html',
    'apps/3DScene/index.html ',
    'apps/StoryMapBasic/index.html',
    'apps/Cascade/index.html',
    'apps/MapJournal/index.html',
    'apps/MapSeries/index.html',
    'apps/MapShortlist/index.html',
    'apps/StorytellingSwipe/index.html',
    'apps/MapTour/index.html',
    'apps/SummaryViewer/index.html ',
    'apps/TimeAware/index.html',
    'apps/ZoneLookup/index.html',
    'apps/Viewer/index.html',
    'apps/OnePane/basicviewer/index.html',
    'apps/Compare/Configure/index.html',
    'apps/Edit/index.html',
    'apps/Elevations/index.html',
    'apps/OnePane/basicviewer/profile.html',
    'apps/Filter/index.html',
    'apps/Embed/index.html',
    'apps/SimpleMapViewer/index.html',
    'apps/SimpleViewer/index.html',
    'apps/OnePane/storytelling_basic/index.html',
    'apps/Time/index.html',
    'apps/OnePane/azuretime/index.html',
    'apps/Solutions/s2.html?appid='
]

# Page through app items to check against list
problem_apps = []
for app in apps:
    if app.url is None: continue
    try:
        if "/apps" in app.url and portal_url in app.url:
            for a in deprecated_portal_apps:
                if a in app.url:
                    problem_apps.append([app.id, app.url, app.numViews])
    except:
        print(f"Issue checking app: {app.title}")
        continue
        

#Sort apps by URL and Print List
problem_apps_sorted = sorted(problem_apps, key = lambda x: x[1], reverse=False)  #To sort by Views descending, change x[1] to x[2]

print(f"{len(problem_apps)} Total Problematic Apps Found:")
print("App ID\t\t\t\t\t|Number of Views\t|App URL")  #Table Header
for p in problem_apps_sorted:
    print(f"{p[0]}\t|{p[2]}\t\t\t|{p[1]}")