logo

scripts

A bunch of scripts, some to be moved to their own repository git clone https://hacktivis.me/git/scripts.git

tweet.py (3833B)


  1. #!/usr/bin/python3
  2. # -*- encoding: utf-8 -*-
  3. from TwitterAPI import TwitterAPI, TwitterOAuth, TwitterRestPager
  4. import sys
  5. o = TwitterOAuth.read_file('credentials.txt')
  6. api = TwitterAPI(
  7. o.consumer_key,
  8. o.consumer_secret,
  9. o.access_token_key,
  10. o.access_token_secret)
  11. def favorite(id):
  12. """ required argument, id """
  13. api.request('favorites/create', {'id': id})
  14. def retweet(id):
  15. """ required argument: id """
  16. api.request('statuses/retweet/:{}'.format(id))
  17. def reply(in_reply_to_status_id, status):
  18. """ required arguments: in_reply_to_status_id, status """
  19. api.request('statuses/update', {'in_reply_to_status_id': in_reply_to_status_id, 'status': status})
  20. def delete(id):
  21. """ required argument: id """
  22. api.request('statuses/destroy/:{}'.format(id))
  23. def nsfw(status):
  24. """ required argument: status """
  25. api.request('statuses/update', {'status': status, 'possibly_sensitive': True})
  26. def upload(file_path, status):
  27. """ required argument: file_path, status """
  28. file = open(file_path, 'rb')
  29. r = api.request('media/upload', None, {'media': file.read()})
  30. api.request('statuses/update', {'status': status, 'media_ids': r.json()['media_id']})
  31. file.close()
  32. def upload_nsfw(file_path, status):
  33. """ required argument: file_path, status """
  34. file = open(file_path, 'rb')
  35. r = api.request('media/upload', None, {'media': file.read()})
  36. api.request('statuses/update', {'status': status, 'media_ids': r.json()['media_id'], 'possibly_sensitive': True})
  37. file.close()
  38. def follow(target):
  39. if target.isalpha():
  40. api.request('friendships/create', {'screen_name': target})
  41. else:
  42. api.request('friendships/create', {'screen_name': target})
  43. def unfollow(target):
  44. if target.isalpha():
  45. api.request('friendships/destroy', {'screen_name': target})
  46. else:
  47. api.request('friendships/destroy', {'user_id': target})
  48. def parse_command(status):
  49. """ Delete: /<del|delete|destroy> <tweet_id> """
  50. """ Favorite & Retweet: /<frt|favrt> <tweet_id> """
  51. """ Favorite: /<f|fav|favorite> <tweet_id> """
  52. """ NSFW: /nsfw <status> """
  53. """ Reply: /<re|reply> <tweet_id> <status> """
  54. """ Retweet: /<rt|retweet> <tweet_id> """
  55. """ Upload: /upload <tweet_id> """
  56. """ Follow: /follow <screen_name|user_id> """
  57. """ Unfollow: /unfollow <screen_name|user_id> """
  58. """ To tweet just put it without any command. Image uploading not supported yet """
  59. """ Protip: To send a direct message prepend MS @<name> or M$ @<name> """
  60. if status[0] == '/':
  61. command = status[1:].split(' ')
  62. if command[1] is not None:
  63. if command[0] in ('f', 'fav', 'favorite'):
  64. favorite(id = command[1])
  65. elif command[0] in ('rt', 'retweet'):
  66. retweet(id = command[1])
  67. elif command[0] in ('frt', 'favrt'):
  68. favorite(id = command[1])
  69. retweet(id = command[1])
  70. elif command[0] in ('re', 'reply'):
  71. reply(in_reply_to_status_id = command[1], status = status.split(' ', 2)[2])
  72. elif command[0] in ('del', 'delete', 'destroy'):
  73. delete(command[1])
  74. elif command[0] == 'nsfw':
  75. nsfw(status = command[1])
  76. elif command[0] == 'upload':
  77. upload(status = status.split(' ', 2)[2], file_path = command[1])
  78. elif command[0] == 'upload_nsfw':
  79. upload(status = status.split(' ', 2)[2], file_path = command[1])
  80. elif command[0] == 'follow':
  81. follow(command[1])
  82. elif command[0] == 'unfollow':
  83. unfollow(command[1])
  84. elif command[0] == 'tweet':
  85. print('No command is used to tweet, just put it without /tweet')
  86. else:
  87. print(status, 'doesn’t exist.')
  88. _help()
  89. else:
  90. print(status, 'you’ve forget arguments.')
  91. _help()
  92. else:
  93. if len(status) > 140:
  94. print('Tweets have a max of 140 charaters')
  95. elif len(status) == 0:
  96. print('Tweets cannot be empty')
  97. else:
  98. print(api.request('statuses/update', {'status': status}))
  99. if __name__ == "__main__":
  100. while 1:
  101. prompt = input('>>')
  102. parse_command(prompt)