File size: 3,798 Bytes
c5cb1c8
 
 
 
 
 
 
 
19d1256
c5cb1c8
 
 
 
 
 
 
6975c18
 
c5cb1c8
 
 
 
 
 
19d1256
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c5cb1c8
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
from git import Repo
import os
import streamlit as st

st.set_page_config(
    page_title="TRACING INSIGHTS",
    page_icon=None,
    layout="wide",
    #initial_sidebar_state="expanded",
    # menu_items={
    #     'Get Help': 'https://www.extremelycoolapp.com/help',
    #     'Report a bug': "https://www.extremelycoolapp.com/bug",
    #     'About': "# This is a header. This is an *extremely* cool app!"
    # }
)

st.write("Hello world!")

GITHUB_PAT = os.environ['GITHUB']
api_key = os.environ['api_key']
secret_api_key = os.environ['secret_api_key']
access_token = os.environ['access_token']
secret_access_token = os.environ['secret_access_token']

# if not os.path.exists('repo_directory'):  
#     Repo.clone_from(f'https://tracinginsights:{GITHUB_PAT}@github.com/TracingInsights/translator.git', 'repo_directory'  )

# from repo_directory import translator
# translator.auth(api_key,secret_api_key,access_token,secret_access_token)




import translators as ts
import translators.server as tss
import random
import tweepy
import time
import json

# works with tweets and replies too

def bulk_trans(text: str, ):
    """
    Will translate str into num_trans different languages and then back into original language
    :param text: str, must    
    :return: str
    """    
    curr = tss.google(text, to_language="en", sleep_seconds=0.051)

    return curr

def check_or_write(filename:str, s1:str):
    """
    Will check if s1 is in a single line within filename and also write 
    the tweet to filename if it is not inside. Returns true when s1 is
    not inside, returns false when s1 is found. Keeps line length of text
    file less than 22
    :param filename: str(must)
    :param s1: str(must)
    :return: bool
    """
    #checks to see if s1 is already inside
    with open(filename, 'r', encoding='utf8') as f1:
        f1.seek(0)
        lines=f1.readlines()
        l=0
        for line in lines:
            l+=1
            if s1 in line:
                return False
    #makes sure that there is not more than 20 lines in txt file
    if l>20:
        while l>20:
            del lines[0]
            l-=1
        new_file=open(filename, "w+", encoding='utf8')
        for line in lines:
            new_file.write(line)
        new_file.close()

    #returns true and also writes down text into file at the end
    with open(filename, 'a', encoding='utf8') as f1:
        f1.write(s1 + '\n')
        f1.close()
        return True

 
# from translator github
def auth(api_key,secret_api_key,access_token,secret_access_token):
    # Authenticate to Twitter
    auth = tweepy.OAuthHandler(api_key, secret_api_key)
    auth.set_access_token(access_token, secret_access_token)
    api = tweepy.API(auth, wait_on_rate_limit=True)

    #loops endlessly(60 sec interval) and checks,translates,and posts tweets
    # use this to get user id https://tweeterid.com/
    while True:
        # get twitter user_id at https://tweeterid.com/ by typing username
        timeline = api.user_timeline(user_id="1568348454619070465",  #testing case using englishgiuly,
                                     #["2755544640", "1030481714", "407048032","227265199","190632194", "149542215","127854979", "106170617"], 
                                     count=5, tweet_mode="extended",)
        for tweet in timeline:
            if check_or_write('tweet.txt', " ".join(tweet.full_text.splitlines())):
                curr = bulk_trans(tweet.full_text)
                status = f"{curr[:254]} https://twitter.com/1/status/{tweet.id}" # each link is converted to 23 characters
                api.update_status(status) # doesn't matter what username we use
                print(tweet.full_text)
        time.sleep(60)

auth(api_key,secret_api_key,access_token,secret_access_token)