bear / python-twitter

A Python wrapper around the Twitter API.
Apache License 2.0
3.41k stars 957 forks source link

Is it possible to get stream data separated by expressions? #622

Closed dhsdshdhk closed 5 years ago

dhsdshdhk commented 5 years ago
def main():
    with open('output.txt', 'a') as f:
        # api.GetStreamFilter will return a generator that yields one status
        # message (i.e., Tweet) at a time as a JSON dictionary.
        for line in api.GetStreamFilter(track=['expA', 'expB'], languages=LANGUAGES):
            f.write(json.dumps(line))
            f.write('\n')

The above code will group all my tracked tweets matching either expression into a single json file. Is it possible to separate them so each expression goes into its own file?

jeremylow commented 5 years ago

I would assume that you could do something like:

import re

def main():
    expA_file = open("expA.json", "w+")
    expB_file = open("expB.json", "w+")
    for line in api.GetStreamFilter(track=['expA', 'expB'], languages=LANGUAGES):
        if re.match(expA, line):
            expA_file.write(json.dumps(line))
            expA_file.write('\n')
        elif re.match(expB, line):
            expB_file.write(json.dumps(line))
            expB_file.write('\n')
    finally:
        expA_file.close()
        expB_file.close()

But that assumes that there's no overlap between matching on expA and expB, though I guess you could handle that in a special case.