Advertisement
Guest User

Untitled

a guest
Dec 5th, 2017
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.13 KB | None | 0 0
  1. # coding=utf-8
  2. """
  3.    instabot example
  4.  
  5.    Workflow:
  6.        Like medias by location.
  7. """
  8.  
  9. import argparse
  10. import os
  11. import sys
  12. import codecs
  13.  
  14. from tqdm import tqdm
  15.  
  16. stdout = sys.stdout
  17. sys.stdout = codecs.getwriter('utf8')(sys.stdout)
  18. sys.path.append(os.path.join(sys.path[0], '../'))
  19. from instabot import Bot
  20.  
  21.  
  22. try:
  23.     input = raw_input
  24. except NameError:
  25.     pass
  26.  
  27.  
  28. def like_location_feed(new_bot, new_location, amount=0):
  29.     counter = 0
  30.     max_id = ''
  31.     with tqdm(total=amount) as pbar:
  32.         while counter < amount:
  33.             if new_bot.getLocationFeed(new_location['location']['pk'], maxid=max_id):
  34.                 location_feed = new_bot.LastJson
  35.                 for media in new_bot.filter_medias(location_feed["items"][:amount], quiet=True):
  36.                     comment_text = random.choice(new_bot.comments)
  37.                     if bot.comment(media, comment_text):
  38.                         counter += 1
  39.                         pbar.update(1)
  40.                 if location_feed.get('next_max_id'):
  41.                     max_id = location_feed['next_max_id']
  42.                 else:
  43.                     return False
  44.     return True
  45.  
  46.  
  47. parser = argparse.ArgumentParser(add_help=True)
  48. parser.add_argument('-u', type=str, help="username")
  49. parser.add_argument('-p', type=str, help="password")
  50. parser.add_argument('-amount', type=str, help="amount")
  51. parser.add_argument('-proxy', type=str, help="proxy")
  52. parser.add_argument('locations', type=str, nargs='*', help='locations')
  53. args = parser.parse_args()
  54.  
  55. try:
  56.     print(u'Like medias by location')
  57. except TypeError:
  58.     sys.stdout = stdout
  59.  
  60. bot = Bot(comments_file="comments.txt")
  61. bot.login(username=args.u, password=args.p,
  62.           proxy=args.proxy)
  63.  
  64. if args.locations:
  65.     for location in args.locations:
  66.         print(u"Location: {}".format(location))
  67.         bot.searchLocation(location)
  68.         finded_location = bot.LastJson['items'][0]
  69.         if finded_location:
  70.             print(u"Found {}".format(finded_location['title']))
  71.  
  72.             if not args.amount:
  73.                 nlikes = input(u"How much likes per location?\n")
  74.             else:
  75.                 nlikes = args.amount
  76.             like_location_feed(bot, finded_location, amount=int(nlikes))
  77. else:
  78.     location_name = input(u"Write location name:\n").strip()
  79.     bot.searchLocation(location_name)
  80.     if not bot.LastJson['items']:
  81.         print(u'Location was not found')
  82.         exit(1)
  83.     if not args.amount:
  84.         nlikes = input(u"How much likes per location?\n")
  85.     else:
  86.         nlikes = args.amount
  87.     ans = True
  88.     while ans:
  89.         for n, location in enumerate(bot.LastJson["items"], start=1):
  90.             print(u'{0}. {1}'.format(n, location['title']))
  91.         print(u'\n0. Exit\n')
  92.         ans = input(u"What place would you want to choose?\n").strip()
  93.         if ans == '0':
  94.             exit(0)
  95.         try:
  96.             ans = int(ans) - 1
  97.             if ans in range(len(bot.LastJson["items"])):
  98.                 like_location_feed(bot, bot.LastJson["items"][ans], amount=int(nlikes))
  99.         except ValueError:
  100.             print(u"\n Not valid choice. Try again")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement