-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathGet-APIHelperFunction.ps1
More file actions
107 lines (81 loc) · 5.43 KB
/
Get-APIHelperFunction.ps1
File metadata and controls
107 lines (81 loc) · 5.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
$ErrorActionPreference = 'Stop'
function Get-APIHelperResources {
$baseUrl = 'https://developer.twitter.com/en/docs/api-reference-index'
Write-Warning $baseUrl
Enter-SeUrl -Driver $Driver -Url $baseUrl
$eApiReferencesIndex = $Driver.FindElementByCssSelector('div[class$="-api-references-index"]')
$ApiResources = $eApiReferencesIndex.FindElementsByCssSelector('a[class$="_link"]').Where({($_.Text -match "GET ") -or ($_.Text -match "POST ")}) |
Select-Object @{n="Method"; e={($_.Text -Split " ")[0]}}, @{n="Resource"; e={"/" + ($_.Text -Split " ")[1]}}, @{n="ReferenceUrl"; e={$_.GetAttribute('href')}}
# Add Missing References from Index page
$ApiResources += [PSCustomObject]@{ Method = 'GET'; Resource = "/application/rate_limit_status"; ReferenceUrl = 'https://developer.twitter.com/en/docs/developer-utilities/rate-limit-status/api-reference/get-application-rate_limit_status' }
$ApiResources += [PSCustomObject]@{ Method = 'GET'; Resource = "/search/tweets"; ReferenceUrl = 'https://developer.twitter.com/en/docs/tweets/search/api-reference/get-search-tweets' }
$ApiResources += [PSCustomObject]@{ Method = 'DELETE'; Resource = "/custom_profiles/destroy"; ReferenceUrl = 'https://developer.twitter.com/en/docs/direct-messages/custom-profiles/api-reference/delete-profile' }
$ApiResources += [PSCustomObject]@{ Method = 'GET'; Resource = "/help/configuration"; ReferenceUrl = 'https://developer.twitter.com/en/docs/developer-utilities/configuration/api-reference/get-help-configuration' }
$ApiResources += [PSCustomObject]@{ Method = 'GET'; Resource = "/help/languages"; ReferenceUrl = 'https://developer.twitter.com/en/docs/developer-utilities/supported-languages/api-reference/get-help-languages' }
$ApiResources += [PSCustomObject]@{ Method = 'GET'; Resource = "/help/privacy"; ReferenceUrl = 'https://developer.twitter.com/en/docs/developer-utilities/privacy-policy/api-reference/get-help-privacy' }
$ApiResources += [PSCustomObject]@{ Method = 'GET'; Resource = "/help/tos"; ReferenceUrl = 'https://developer.twitter.com/en/docs/developer-utilities/terms-of-service/api-reference/get-help-tos' }
}
function Get-APIHelperFunction ($ApiResource) {
Write-Warning $ApiResource.ReferenceUrl
Enter-SeUrl -Driver $Driver -Url $ApiResource.ReferenceUrl
Try { $eResourceReference = $Driver.FindElementByCssSelector('div[class="c01-rich-text-editor"]').FindElementByCssSelector('div[class="toctree"]').FindElementsByXPath("*") } Catch { }
If ($eResourceReference.Text -notcontains 'Resource URL') {
Try { $eResourceReference = $Driver.FindElementByCssSelector('div[class="c01-rich-text-editor"]').FindElementByTagName("div").FindElementsByXPath("*") } Catch { }
If ($eResourceReference.Text -notcontains 'Resource URL') {
Write-Warning 'Resource URL not found.'
Return $ApiResource
}
}
$eResource = @{}
$Parameter = "Description"
$eResource[$Parameter] = @()
ForEach($eReference in $eResourceReference) {
Switch ($eReference.Text) {
"Resource URL" { $Parameter = "ResourceUrl"; $SkipLine = $True }
"Resource Information" { $Parameter = "ResourceInformation"; $SkipLine = $True }
"Resource Infromation" { $Parameter = "ResourceInformation"; $SkipLine = $True }
"Parameters" { $Parameter = "Parameters"; $SkipLine = $True }
"Example Request" { $parameter = "ExampleRequest"; $SkipLine = $True }
"Example Response" { $parameter = "ExampleResponse"; $SkipLine = $True }
Default { $SkipLine = $False }
}
If ($SkipLine) { $eResource[$Parameter] = @(); Continue }
$eResource[$Parameter] += $eReference
}
$Result = @{}
$Result['fDescription'] = $Driver.FindElementByTagName("h1").Text
ForEach($Key in $eResource.Keys) {
Switch ($Key) {
'Parameters' {
$Parameters = @()
$eParameters = $eResource[$Key].FindElementsByTagName('tr')
ForEach($eParameter in $eParameters) {
$ParameterColumns = $eParameter.FindElementsByTagName('td').Text
If (-Not($ParameterColumns)) { Continue }
If ($ParameterColumns[0] -eq 'Name') { Continue }
$Parameters += [PSCustomObject]@{
Name = $ParameterColumns[0]
Required = $ParameterColumns[1]
Description = $ParameterColumns[2]
DefaultValue = $ParameterColumns[3]
Example = $ParameterColumns[4]
}
}
$Result[$Key] = $Parameters
}
Default {
$Result[$Key] = ( $eResource[$Key].Text | ? { $_ } ) -Join "`r`n`r`n"
}
}
}
$ApiResource | Add-Member -MemberType NoteProperty -Name ReferenceProperties -Value ([PSCustomObject]$Result) -Force
}
Import-Module Selenium
$Driver = Start-SeChrome
#$ApiResources = Get-Content ".\twitter_api.json" | ConvertFrom-Json ## Used for Debuging
$ApiResources = Get-APIHelperResources
#ForEach($ApiResource in ($ApiResources | Where-Object { -Not($_.ReferenceProperties.Parameters.Count -gt 0) })) { ## Used for Debuging
ForEach($ApiResource in $ApiResources) {
$ApiResource = Get-APIHelperFunction -ApiResource $ApiResource
}
$ApiResources | ConvertTo-Json -Depth 5 | Set-Content ".\twitter_api.json"