Esempio n. 1
0
class Application:
    def __init__(self, username, password):
        self.username = username
        self.password = password
        self.instagram = Instagram(username, password)
        self.logged_in = False

    def exit_application(self):
        if self.logged_in:
            self.instagram.logout()

    def thread_list(self, sleep_time=2):
        next_page = ''
        while True:
            time.sleep(sleep_time)

            direct = self.instagram.direct_list(next_page=next_page)
            if direct:
                items = direct['inbox']['threads']
                for item in items:
                    yield item['thread_title']

                if not direct['inbox']['has_older']:
                    return

                next_page = direct['inbox']['oldest_cursor']

    def run(self):
        if not self.instagram.login():
            self.exit_application()
            return

        self.logged_in = True
        for thread_name in self.thread_list():
            print thread_name
Esempio n. 2
0
class Application:
    def __init__(self, username, password, thread_name, output_dir,
                 debug_mode=False):
        self.username = username
        self.password = password
        self.selected_thread_name = thread_name
        self.logged_in = False
        self.debug_mode = debug_mode
        self.media_folder = output_dir
        self.instagram = Instagram(username, password, debug_mode=debug_mode)
        self.selected_thread_id = ''

        if not os.path.exists(output_dir):
            os.mkdir(output_dir, 0755)

        self.dump_file = open(os.path.join(output_dir, 'dump_file.csv'), 'wb')
        self.csv_handler = csv.writer(self.dump_file, delimiter=',', quotechar='|')

    def exit_application(self, error):
        if self.debug_mode:
            logging.error(error)
        self.dump_file.close()
        if self.logged_in:
            self.instagram.logout()

    def find_thread_id(self, thread_title):
        next_page = ''
        while True:
            direct = self.instagram.direct_list(next_page=next_page)
            if direct:
                items = direct['inbox']['threads']
                for item in items:
                    if item['thread_title'] == thread_title:
                        return item['thread_id']

                if not direct['inbox']['has_older']:
                    return

                next_page = direct['inbox']['oldest_cursor']

    @staticmethod
    def download(url, target):
        if os.path.exists(target):
            return
        image_file = urllib2.urlopen(url)
        with open(target, 'wb') as output:
            output.write(image_file.read())

    def dump_message(self, message):
        if message['item_type'] == 'text':
            text_message = safe_string(message['text'])
        else:
            text_message = message['item_type']
        times = datetime.fromtimestamp(message['timestamp']/1000000)
 
        self.csv_handler.writerow([message['user_id'], text_message, times])

    def thread_message_generator(self):
        next_page = ''
        while True:
            thread = self.instagram.direct_thread(self.selected_thread_id, next_page=next_page)
            if not thread:
                self.exit_application('Could not select thread')
                return

            for message in thread['thread']['items']:
                yield message

            if not thread['thread']['has_older']:
                return

            next_page = thread['thread']['oldest_cursor']

    def run(self):
        if self.debug_mode:
            logging.info('Logging into {}'.format(self.username))

        if not self.instagram.login():
            self.exit_application('Login failed')
            return

        self.logged_in = True
        self.selected_thread_id = self.find_thread_id(self.selected_thread_name)
        if not self.selected_thread_id:
            self.exit_application('Could not find thread_id')
            return

        if self.debug_mode:
            logging.info(
                'Thread id for {} has been founded, id={}'.format(self.selected_thread_name, self.selected_thread_id))
        self.csv_handler.writerow(["userid", "message", "date"])
        for message in self.thread_message_generator():
            if message['item_type'] == 'media':
                media_type = message['media']['media_type']
                if media_type == 1:
                    self.download(message['media']['image_versions2']['candidates'][0]['url'],
                                  os.path.join(self.media_folder, message['item_id'] + '.jpg'))
                elif media_type == 2:
                    self.download(message['media']['video_versions'][0]['url'],
                                  os.path.join(self.media_folder, message['item_id'] + '.mp4'))
            else:
                self.dump_message(message)

    def remove_messages(self, reverse=False):
        name = self.dump_file.name
        self.dump_file.close()

        self.dump_file = open(name, 'rb')
        self.csv_handler = csv.reader(self.dump_file)

        items = [row for row in self.csv_handler]
        items.sort(key=lambda a: a[3], reverse=reverse)

        for message in items:
            if int(message[0]) == self.instagram.username_id:
                result = self.instagram.delete_direct_message(self.selected_thread_id, message[2])
                if not self.debug_mode:
                    continue

                if result:
                    if result["status"] == "ok":
                        logging.info("Direct item {} has been deleted.".format(message[2]))
                    else:
                        logging.error(result["status"])
                else:
                    logging.error("Could not remove direct item {}.".format(message[2]))
from instagram import Instagram
from selenium import webdriver
from time import sleep
from comment_list import COMMENT_LIST

URL = "https://www.instagram.com/p/CCZfJc9jbFz/"
USERNAME = "******"
PASSWORD = "******"
COMMENT_NUMBER = 1000

COMMENT_LIST = COMMENT_LIST

USER_NUMBER_PER_COMMENT = 2

DRIVER = webdriver.Chrome()

INSTAGRAM = Instagram(DRIVER, URL)
INSTAGRAM.navigate()
INSTAGRAM.login(USERNAME, PASSWORD)
INSTAGRAM.comment(COMMENT_LIST, USER_NUMBER_PER_COMMENT, COMMENT_NUMBER)

Esempio n. 4
0
from selenium import webdriver
import random
from instagram import Instagram
from env import USERNAME, PASSWORD

try:
    instagram = Instagram()

    instagram.open()

    instagram.login(USERNAME, PASSWORD)

    while True:
        instagram.wait()
        instagram.follow_followers(USERNAME)
        instagram.wait()
        instagram.like_photos_with_tag_list(
            ["puppy", "pequines", "cachorro", "animal", "dog", "cute"])

        # q = input("Press:\n1) Follow Followers\n2) Like photos by Tag list\n0) Quit\n")

        # if q == "1":
        #    instagram.follow_followers(USERNAME)

        # elif q == "2":
        #    instagram.like_photos_with_tag_list(["puppy", "pequines", "cachorro", "animal", "dog", "cute"])

        # else:
        #    break
except Exception as e:
    print("Aconteceu um erro: ")
Esempio n. 5
0
                     twitter.follow()
                     twitter.refresh()
                 else:
                     twitter.like()
         except Exception as e:
             print(e)
         finally:
             # twitter.quit()
             random_sleep(300, 600)
             i += 1
 else:
     instagram = Instagram()
     while True:
         keyword = KEYWORDS[random.randrange(len(KEYWORDS))]
         try:
             instagram.login()
             instagram.search(keyword)
             if args.follow:
                 instagram.follow()
                 if i % refresh_point == 0:
                     instagram.refresh()
             else:
                 if i % refresh_point == 0:
                     instagram.like()
                     instagram.follow()
                     instagram.refresh()
                 else:
                     instagram.like()
         except Exception as e:
             print(e)
         finally:
Esempio n. 6
0
    detector = Detector(
        join_path('models', 'shape_predictor_68_face_landmarks.dat'),
        join_path('models', 'dlib_face_recognition_resnet_model_v1.dat'),
        join_path('models', 'gender.pickle'),
    )
    graph = Graph(os.getenv('INSTAGRAM_NEO4J'))

    instagram = Instagram(
        os.getenv('INSTAGRAM_USERNAME'),
        os.getenv('INSTAGRAM_PASSWORD'),
    )
    current_user = os.getenv('INSTAGRAM_USERNAME')
    current_gender = 'unknown'

    log_event('info', 'trying to connect to Instagram ...')
    if not instagram.login():
        log_event('error', "couldn't connect to Instagram !")
        sys.exit(1)

    log_event('info', 'fetching logged-in user followers ...')
    followers = instagram.followers(instagram.username_id)
    if followers['status'] != 'ok':
        log_event('error', 'invalid Instagram status ' + followers['status'])
        sys.exit(2)

    for follower in tqdm(shuffle_list(followers['users'], limit=limit_users)):
        username = follower['username']
        username_id = follower['pk']

        try:
            image = url_to_image(follower['profile_pic_url'])