add GetAccountSettings and GetAccountList
This commit is contained in:
parent
807bccc9f5
commit
7a9256072f
3 changed files with 91 additions and 117 deletions
|
|
@ -3,6 +3,7 @@
|
||||||
## v0.0.13
|
## v0.0.13
|
||||||
|
|
||||||
- Added methods `GetTweetsAndReplies`, `FetchTweetsAndReplies`, `FetchTweetsAndRepliesByUserID` thanks to @thewh1teagle
|
- Added methods `GetTweetsAndReplies`, `FetchTweetsAndReplies`, `FetchTweetsAndRepliesByUserID` thanks to @thewh1teagle
|
||||||
|
- Added methods `GetAccountSettings` and `GetAccountList` thanks to @thewh1teagle
|
||||||
|
|
||||||
## v0.0.12
|
## v0.0.12
|
||||||
|
|
||||||
|
|
|
||||||
172
settings.go
172
settings.go
|
|
@ -1,129 +1,67 @@
|
||||||
package twitterscraper
|
package twitterscraper
|
||||||
|
|
||||||
import (
|
type AccountSettings struct {
|
||||||
"fmt"
|
ScreenName string `json:"screen_name"`
|
||||||
"net/http"
|
Protected bool `json:"protected"`
|
||||||
"net/url"
|
DisplaySensitiveMedia bool `json:"display_sensitive_media"`
|
||||||
)
|
Language string `json:"language"`
|
||||||
|
CountryCode string `json:"country_code"`
|
||||||
|
|
||||||
type UserSettings struct {
|
DiscoverableByEmail bool `json:"discoverable_by_email"`
|
||||||
Protected bool `json:"protected"`
|
DiscoverableByMobilePhone bool `json:"discoverable_by_mobile_phone"`
|
||||||
ScreenName string `json:"screen_name"`
|
PersonalizedTrends bool `json:"personalized_trends"`
|
||||||
AlwaysUseHTTPS bool `json:"always_use_https"`
|
AllowMediaTagging string `json:"allow_media_tagging"`
|
||||||
UseCookiePersonalization bool `json:"use_cookie_personalization"`
|
AllowContributorRequest string `json:"allow_contributor_request"`
|
||||||
SleepTime interface{} `json:"sleep_time"`
|
AllowAdsPersonalization bool `json:"allow_ads_personalization"`
|
||||||
GeoEnabled bool `json:"geo_enabled"`
|
AllowLoggedOutDevicePersonalization bool `json:"allow_logged_out_device_personalization"`
|
||||||
Language string `json:"language"`
|
AllowLocationHistoryPersonalization bool `json:"allow_location_history_personalization"`
|
||||||
DiscoverableByEmail bool `json:"discoverable_by_email"`
|
AllowSharingDataForThirdPartyPersonalization bool `json:"allow_sharing_data_for_third_party_personalization"`
|
||||||
DiscoverableByMobilePhone bool `json:"discoverable_by_mobile_phone"`
|
AllowDmsFrom string `json:"allow_dms_from"`
|
||||||
DisplaySensitiveMedia bool `json:"display_sensitive_media"`
|
AllowDmGroupsFrom string `json:"allow_dm_groups_from"`
|
||||||
PersonalizedTrends bool `json:"personalized_trends"`
|
AddressBookLiveSyncEnabled bool `json:"address_book_live_sync_enabled"`
|
||||||
AllowMediaTagging string `json:"allow_media_tagging"`
|
UniversalQualityFilteringEnabled string `json:"universal_quality_filtering_enabled"`
|
||||||
AllowContributorRequest string `json:"allow_contributor_request"`
|
DmReceiptSetting string `json:"dm_receipt_setting"`
|
||||||
AllowAdsPersonalization bool `json:"allow_ads_personalization"`
|
AllowAuthenticatedPeriscopeRequests bool `json:"allow_authenticated_periscope_requests"`
|
||||||
AllowLoggedOutDevicePersonalization bool `json:"allow_logged_out_device_personalization"`
|
ProtectPasswordReset bool `json:"protect_password_reset"`
|
||||||
AllowLocationHistoryPersonalization bool `json:"allow_location_history_personalization"`
|
RequirePasswordLogin bool `json:"require_password_login"`
|
||||||
AllowSharingDataForThirdPartyPersonalization bool `json:"allow_sharing_data_for_third_party_personalization"`
|
RequiresLoginVerification bool `json:"requires_login_verification"`
|
||||||
AllowDMsFrom string `json:"allow_dms_from"`
|
DmQualityFilter string `json:"dm_quality_filter"`
|
||||||
AlwaysAllowDMsFromSubscribers interface{} `json:"always_allow_dms_from_subscribers"`
|
AutoplayDisabled bool `json:"autoplay_disabled"`
|
||||||
AllowDMGroupsFrom string `json:"allow_dm_groups_from"`
|
|
||||||
TranslatorType string `json:"translator_type"`
|
|
||||||
CountryCode string `json:"country_code"`
|
|
||||||
NsfwUser bool `json:"nsfw_user"`
|
|
||||||
NsfwAdmin bool `json:"nsfw_admin"`
|
|
||||||
RankedTimelineSetting interface{} `json:"ranked_timeline_setting"`
|
|
||||||
RankedTimelineEligible interface{} `json:"ranked_timeline_eligible"`
|
|
||||||
AddressBookLiveSyncEnabled bool `json:"address_book_live_sync_enabled"`
|
|
||||||
UniversalQualityFilteringEnabled string `json:"universal_quality_filtering_enabled"`
|
|
||||||
DMReceiptSetting string `json:"dm_receipt_setting"`
|
|
||||||
AltTextComposeEnabled interface{} `json:"alt_text_compose_enabled"`
|
|
||||||
MentionFilter string `json:"mention_filter"`
|
|
||||||
AllowAuthenticatedPeriscopeRequests bool `json:"allow_authenticated_periscope_requests"`
|
|
||||||
ProtectPasswordReset bool `json:"protect_password_reset"`
|
|
||||||
RequirePasswordLogin bool `json:"require_password_login"`
|
|
||||||
RequiresLoginVerification bool `json:"requires_login_verification"`
|
|
||||||
ExtSharingAudioSpacesListeningDataWithFollowers bool `json:"ext_sharing_audiospaces_listening_data_with_followers"`
|
|
||||||
Ext interface{} `json:"ext"`
|
|
||||||
DMQualityFilter string `json:"dm_quality_filter"`
|
|
||||||
AutoplayDisabled bool `json:"autoplay_disabled"`
|
|
||||||
SettingsMetadata struct{} `json:"settings_metadata"` // Empty struct since the original value is an empty object
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// parseUserSettings parses the JSON response into the UserSettings struct.
|
type Account struct {
|
||||||
func parseUserSettings(jsn map[string]interface{}) UserSettings {
|
UserID string `json:"user_id"`
|
||||||
return UserSettings{
|
Name string `json:"name"`
|
||||||
Protected: jsn["protected"].(bool),
|
ScreenName string `json:"screen_name"`
|
||||||
ScreenName: jsn["screen_name"].(string),
|
AvatarImageURL string `json:"avatar_image_url"`
|
||||||
AlwaysUseHTTPS: jsn["always_use_https"].(bool),
|
IsSuspended bool `json:"is_suspended"`
|
||||||
UseCookiePersonalization: jsn["use_cookie_personalization"].(bool),
|
IsVerified bool `json:"is_verified"`
|
||||||
SleepTime: jsn["sleep_time"],
|
IsProtected bool `json:"is_protected"`
|
||||||
GeoEnabled: jsn["geo_enabled"].(bool),
|
IsAuthValid bool `json:"is_auth_valid"`
|
||||||
Language: jsn["language"].(string),
|
|
||||||
DiscoverableByEmail: jsn["discoverable_by_email"].(bool),
|
|
||||||
DiscoverableByMobilePhone: jsn["discoverable_by_mobile_phone"].(bool),
|
|
||||||
DisplaySensitiveMedia: jsn["display_sensitive_media"].(bool),
|
|
||||||
PersonalizedTrends: jsn["personalized_trends"].(bool),
|
|
||||||
AllowMediaTagging: jsn["allow_media_tagging"].(string),
|
|
||||||
AllowContributorRequest: jsn["allow_contributor_request"].(string),
|
|
||||||
AllowAdsPersonalization: jsn["allow_ads_personalization"].(bool),
|
|
||||||
AllowLoggedOutDevicePersonalization: jsn["allow_logged_out_device_personalization"].(bool),
|
|
||||||
AllowLocationHistoryPersonalization: jsn["allow_location_history_personalization"].(bool),
|
|
||||||
AllowSharingDataForThirdPartyPersonalization: jsn["allow_sharing_data_for_third_party_personalization"].(bool),
|
|
||||||
AllowDMsFrom: jsn["allow_dms_from"].(string),
|
|
||||||
AlwaysAllowDMsFromSubscribers: jsn["always_allow_dms_from_subscribers"],
|
|
||||||
AllowDMGroupsFrom: jsn["allow_dm_groups_from"].(string),
|
|
||||||
TranslatorType: jsn["translator_type"].(string),
|
|
||||||
CountryCode: jsn["country_code"].(string),
|
|
||||||
NsfwUser: jsn["nsfw_user"].(bool),
|
|
||||||
NsfwAdmin: jsn["nsfw_admin"].(bool),
|
|
||||||
RankedTimelineSetting: jsn["ranked_timeline_setting"],
|
|
||||||
RankedTimelineEligible: jsn["ranked_timeline_eligible"],
|
|
||||||
AddressBookLiveSyncEnabled: jsn["address_book_live_sync_enabled"].(bool),
|
|
||||||
UniversalQualityFilteringEnabled: jsn["universal_quality_filtering_enabled"].(string),
|
|
||||||
DMReceiptSetting: jsn["dm_receipt_setting"].(string),
|
|
||||||
AltTextComposeEnabled: jsn["alt_text_compose_enabled"],
|
|
||||||
MentionFilter: jsn["mention_filter"].(string),
|
|
||||||
AllowAuthenticatedPeriscopeRequests: jsn["allow_authenticated_periscope_requests"].(bool),
|
|
||||||
ProtectPasswordReset: jsn["protect_password_reset"].(bool),
|
|
||||||
RequirePasswordLogin: jsn["require_password_login"].(bool),
|
|
||||||
RequiresLoginVerification: jsn["requires_login_verification"].(bool),
|
|
||||||
ExtSharingAudioSpacesListeningDataWithFollowers: jsn["ext_sharing_audiospaces_listening_data_with_followers"].(bool),
|
|
||||||
Ext: jsn["ext"],
|
|
||||||
DMQualityFilter: jsn["dm_quality_filter"].(string),
|
|
||||||
AutoplayDisabled: jsn["autoplay_disabled"].(bool),
|
|
||||||
SettingsMetadata: struct{}{}, // Empty struct
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetProfile return parsed user profile.
|
type AccountList struct {
|
||||||
func (s *Scraper) GetUserSettings(authToken AuthToken) (UserSettings, error) {
|
Users []Account `json:"users"`
|
||||||
var jsn map[string]interface{}
|
}
|
||||||
req, err := http.NewRequest("GET", "https://api.x.com/1.1/account/settings.json", nil)
|
|
||||||
|
|
||||||
|
func (s *Scraper) GetAccountSettings() (AccountSettings, error) {
|
||||||
|
var settings AccountSettings
|
||||||
|
req, err := s.newRequest("GET", "https://api.twitter.com/1.1/account/settings.json")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return UserSettings{}, err
|
return settings, err
|
||||||
}
|
}
|
||||||
|
|
||||||
query := url.Values{}
|
err = s.RequestAPI(req, &settings)
|
||||||
query.Set("include_ext_sharing_audiospaces_listening_data_with_followers", "true")
|
return settings, err
|
||||||
query.Set("include_mention_filter", "true")
|
}
|
||||||
query.Set("include_nsfw_user_flag", "true")
|
|
||||||
query.Set("include_nsfw_admin_flag", "true")
|
func (s *Scraper) GetAccountList() ([]Account, error) {
|
||||||
query.Set("include_ranked_timeline", "true")
|
var list AccountList
|
||||||
query.Set("include_alt_text_compose", "true")
|
req, err := s.newRequest("GET", "https://api.twitter.com/1.1/account/multi/list.json")
|
||||||
query.Set("ext", "ssoConnections")
|
if err != nil {
|
||||||
query.Set("include_country_code", "true")
|
return list.Users, err
|
||||||
query.Set("include_ext_dm_nsfw_media_filter", "true")
|
}
|
||||||
req.URL.RawQuery = query.Encode()
|
|
||||||
|
err = s.RequestAPI(req, &list)
|
||||||
req.Header.Set("Cookie", fmt.Sprintf("auth_token=%s; ct0=%s", authToken.Token, authToken.CSRFToken))
|
return list.Users, err
|
||||||
req.Header.Set("X-CSRF-Token", authToken.CSRFToken)
|
|
||||||
|
|
||||||
err = s.RequestAPI(req, &jsn)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return UserSettings{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return parseUserSettings(jsn), nil
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
35
settings_test.go
Normal file
35
settings_test.go
Normal file
|
|
@ -0,0 +1,35 @@
|
||||||
|
package twitterscraper_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGetAccountSettings(t *testing.T) {
|
||||||
|
if skipAuthTest {
|
||||||
|
t.Skip("Skipping test due to environment variable")
|
||||||
|
}
|
||||||
|
|
||||||
|
settings, err := testScraper.GetAccountSettings()
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if settings.ScreenName == "" {
|
||||||
|
t.Error("ScreenName is empty")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetAccountList(t *testing.T) {
|
||||||
|
if skipAuthTest {
|
||||||
|
t.Skip("Skipping test due to environment variable")
|
||||||
|
}
|
||||||
|
|
||||||
|
accounts, err := testScraper.GetAccountList()
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(accounts) < 1 {
|
||||||
|
t.Errorf("Returned %d accounts", len(accounts))
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue