-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
62 lines (54 loc) · 2.32 KB
/
main.py
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
import argparse
import twitter
import yaml
from twitterbyconfig.models import (
TwitterUser,
TwitterList,
MetaList,
TwitterAccount,
)
from twitterbyconfig.accountmerger import (
AccountMerger,
)
def CreateApi():
with open('secrets.yaml', 'r') as stream:
try:
secrets = yaml.safe_load(stream)
return twitter.Api(consumer_key=secrets['consumer_key'],
consumer_secret=secrets['consumer_secret'],
access_token_key=secrets['access_token_key'],
access_token_secret=secrets['access_token_secret'])
except yaml.YAMLError as e:
print('Error loading secrets.yaml: {0}'.format(e))
return None
parser = argparse.ArgumentParser(
description='Provides Twitter account management using a plaintext config file.')
parser.add_argument('operation', type=str, choices=['download', 'sync'],
help=('The operation to perform: \n'
' download: downloads account data from Twitter'
' and outputs to your config file\n'
' upload: updates account data in Twitter to'
' match your config file\n'))
parser.add_argument('config_file', type=str, help='The address of your config file.')
if __name__ == '__main__':
args = parser.parse_args()
api = CreateApi()
if args.operation == 'download':
print('Performing download from TwitterAPI into config file...')
account = TwitterAccount.FromApi(api)
print('Account data downloaded, writing to file...')
account.WriteToConfig(args.config_file)
print('File updated from TwitterAPI source: {0}'.format(args.config_file))
elif args.operation == 'sync':
print('Reading account data from config file...')
config_account = TwitterAccount.ReadFromConfig(args.config_file)
print('Reading account data from Twitter API...')
api_account = TwitterAccount.FromApi(api)
account_merger = AccountMerger(api)
merged_account = account_merger.MergeAccounts(api_account, config_account)
write_back_to_config = (
input('Write back canonical follows/lists to config file? y/n: ') == 'y')
if write_back_to_config:
merged_account.WriteToConfig(args.config_file)
else:
raise ValueError('Unsupported operation: {0}'.format(args.operation))