def init(): lat, lng, city = float(settings['location']['latitude']), float( settings['location']['longitude']), settings['location']['name'] access_token = get_config('access_token') expiration_date = get_config('expiration_date') refresh_token = get_config('refresh_token') device_uid = get_config('device_uid') distinct_id = get_config('distinct_id') tokens_changed = False if access_token is None and expiration_date is None and refresh_token is None and device_uid is None and distinct_id is None: logger.debug('Creating new jodel account: lat=%s, lng=%s, city=%s' % (lat, lng, city)) j = jodel_api.JodelAccount(lat=lat, lng=lng, city=city) tokens_changed = True else: j = jodel_api.JodelAccount(lat=lat, lng=lng, city=city, access_token=access_token, expiration_date=expiration_date, refresh_token=refresh_token, device_uid=device_uid, distinct_id=distinct_id, is_legacy=False, update_location=False) if datetime.fromtimestamp(int(expiration_date)) < datetime.now(): logger.debug( 'Expiration date %s has passed, refreshing access token' % (expiration_date, )) result = j.refresh_access_token() if result[0] != '200': logger.error( 'Unable to refresh access token, refresh all tokens') try: result = j.refresh_all_tokens(pushToken=None) except Exception: logger.exception('Unable to refresh all tokens') return None tokens_changed = True if tokens_changed: account_data = j.get_account_data() for token in ('access_token', 'expiration_date', 'refresh_token', 'device_uid', 'distinct_id'): set_config(token, account_data[token]) return j
def create_new_account(account_id='jodel_account'): """Creates a new Jodel account.""" lat, lng, city = 48.148434, 11.567867, "Munich" j = jodel_api.JodelAccount(lat=lat, lng=lng, city=city) j_account_data = j.get_account_data() pickle.dump(j_account_data, open(account_id + '.pkl', 'wb'))
def poll_replies(our_id): jodel = jodel_api.JodelAccount(lat=JODEL_DEFAULT_LAT, lng=JODEL_DEFAULT_LNG, city=DEFAULT_CITY, update_location=False, access_token=JODEL_ACCESS_TOKEN, device_uid=JODEL_DEVICE_UID, refresh_token=JODEL_REFRESH_TOKEN, distinct_id=JODEL_DISTRINCT_ID, expiration_date=JODEL_EXPIRATION_DATE) post_id = db.get(our_id) if post_id: r = jodel.get_post_details_v3(post_id) if r[0] != 200: return HTTPError(500, "Ausgejodelt.") replies = r[1]["replies"] response.content_type = "application/json" return json.dumps([{ k: v for k, v in reply.items() if k in ["message", "poll_vote", "created_at", "updated_at", "distance"] } for reply in replies if reply["post_type"] != "automatic_reply"], ensure_ascii=False) else: return HTTPError(404, "Poll not found")
def setup_class(self): acc = {'device_uid': os.environ.get("JODEL_ACCOUNT_LEGACY")} self.j = jodel_api.JodelAccount(lat, lng, city, update_location=False, is_legacy=True, **acc) assert self.j.get_account_data()['is_legacy'] assert self.j.set_location(lat, lng, city)[0] == 204 # get two post_ids for further testing r = self.j.get_posts_discussed() assert r[0] == 200 assert "posts" in r[1] and "post_id" in r[1]["posts"][0] self.pid1 = r[1]['posts'][0]['post_id'] self.pid2 = r[1]['posts'][1]['post_id'] print(self.pid1, self.pid2) # make sure get_my_pinned() isn't empty pinned = self.j.get_my_pinned_posts() assert pinned[0] == 200 if len(pinned[1]["posts"]) < 5: for post in r[1]["posts"][4:9]: self.j.pin(post["post_id"]) # follow the channel so we can post to it assert self.j.follow_channel(test_channel)[0] == 204
def __init__(self, lat, lon, city, account_data): self.city = city self.j = jodel_api.JodelAccount(lat=lat, lng=lon, city=city, update_location=False, **account_data)
def refresh_all(file_data, filename): jodel_account = jodel_api.JodelAccount( lat=file_data.lat, lng=file_data.lng, city=file_data.city, access_token=file_data.access_token, expiration_date=file_data.expiration_date, refresh_token=file_data.refresh_token, distinct_id=file_data.distinct_id, device_uid=file_data.device_uid, is_legacy=file_data.legacy, update_location=False) jodel_account.refresh_all_tokens() refreshed_account = jodel_account.get_account_data() expiration_date = refreshed_account["expiration_date"] distinct_id = refreshed_account["distinct_id"] refresh_token = refreshed_account["refresh_token"] device_uid = refreshed_account["device_uid"] access_token = refreshed_account["access_token"] filedata = create_data(file_data.lat, file_data.lng, file_data.city, access_token, expiration_date, refresh_token, distinct_id, device_uid, file_data.API_KEY, file_data.CITY, file_data.legacy, file_data.pollen_region, file_data.pollen_partregion) write_data(filedata, filename) return jodel_account
def is_key_working(key, version): try: lat, lng, city = 48.148900, 11.567400, "Munich" j = jodel_api.JodelAccount(lat=lat, lng=lng, city=city) return {'working': True, 'account': j.get_account_data()} except Exception as e: print(e) return {'working': False}
def setup_class(self): self.j = jodel_api.JodelAccount(lat + uniform(-0.01, 0.01), lng + uniform(-0.01, 0.01), city) assert isinstance(self.j, jodel_api.JodelAccount) r = self.j.get_posts_discussed() assert r[0] == 200 assert "posts" in r[1] and "post_id" in r[1]["posts"][0] self.pid = r[1]['posts'][0]['post_id']
def poll(our_id): jodel = jodel_api.JodelAccount(lat=JODEL_DEFAULT_LAT, lng=JODEL_DEFAULT_LNG, city=DEFAULT_CITY, update_location=False, access_token=JODEL_ACCESS_TOKEN, device_uid=JODEL_DEVICE_UID, refresh_token=JODEL_REFRESH_TOKEN, distinct_id=JODEL_DISTRINCT_ID, expiration_date=JODEL_EXPIRATION_DATE) post_id = db.get(our_id) if post_id: r = jodel.get_post_details(post_id) if r[0] != 200: return HTTPError(500, "Ausgejodelt.") post_details = r[1] response.content_type = "application/json" return json.dumps( { k: v for k, v in post_details.items() if k in [ "message", "poll_options", "poll_votes", "created_at", "updated_at", "votable" ] }, ensure_ascii=False) elif set(request.params.keys()) == set( ["city", "lat", "lng", "channel", "message", "poll_option"]): r = jodel.set_location(float(request.params.lat), float(request.params.lng), request.params.city) if r[0] != 204: return HTTPError(500, "Ausgejodelt.") r = jodel.create_post( channel=request.params.channel, message=request.params.message, poll_options=request.params.getall("poll_option")) if r[0] != 200: return HTTPError(500, "Ausgejodelt.") if r[1] == {}: return HTTPError(500, "Ausgejodelt.") post_details = r[1]["post"] db.set(our_id, post_details["post_id"]) response.content_type = "application/json" return json.dumps( { k: v for k, v in post_details.items() if k in [ "message", "channel", "poll_options", "poll_votes", "created_at", "updated_at", "votable" ] }, ensure_ascii=False) else: return HTTPError(404, "Poll not found")
def test_reinitalize(self): acc = self.j.get_account_data() with pytest.raises(Exception) as excinfo: j2 = jodel_api.JodelAccount(lat="a", lng="b", city=13, update_location=True, **acc) assert "Error updating location" in str(excinfo.value)
def setup_class(self): self.j = jodel_api.JodelAccount(lat + uniform(-0.01, 0.01), lng + uniform(-0.01, 0.01), city) assert isinstance(self.j, jodel_api.JodelAccount) r = self.j.get_posts_discussed() assert r[0] == 200 assert "posts" in r[1] and "post_id" in r[1]["posts"][0] self.pid = r[1]['posts'][0]['post_id'] self.pid1 = r[1]['posts'][0]['post_id'] self.pid2 = r[1]['posts'][1]['post_id'] assert self.j.follow_channel(test_channel)[0] == 204 assert self.j.get_account_data()['is_legacy'] == False
def login(lat, lng, city, account_id='jodel_account', update_location=True): """"Login to Jodel based on a pickled account file.""" j_account_data = pickle.load(open(account_id + '.pkl', 'rb')) return jodel_api.JodelAccount( lat=lat, lng=lng, city=city, access_token=j_account_data['access_token'], expiration_date=j_account_data['expiration_date'], refresh_token=j_account_data['refresh_token'], distinct_id=j_account_data['distinct_id'], device_uid=j_account_data['device_uid'], is_legacy=True, update_location=update_location)
def setup_class(self): self.j = jodel_api.JodelAccount(lat, lng, city, update_location=False, **self.acc) r = self.j.refresh_all_tokens() assert r[0] == 200 r = self.j.get_posts_discussed() assert r[0] == 200 assert "posts" in r[1] and "post_id" in r[1]["posts"][0] self.pid1 = r[1]['posts'][0]['post_id'] self.pid2 = r[1]['posts'][1]['post_id'] assert self.j.follow_channel("WasGehtHeute?")[0] == 204
def create_account(lat, lng, city): account = jodel_api.JodelAccount(lat=lat, lng=lng, city=city) while True: try: a = jodel_api.AndroidAccount() account.verify(a) account.get_account_data() break except: raise Exception( "Couldn't verify account. Please retry in a few Minutes.") return InputJodelAcc(access_token=account.access_token, expiration_date=account.expiration_date, refresh_token=account.refresh_token, distinct_id=account.distinct_id, device_uid=account.device_uid, legacy=False)
def create_account(): # Get location from user. print("Input the location your Jodels should be posted at:\n") lat = input("Latitude:\n") lng = input("Longitude:\n") city = input("City:\n") # Try for jodel account print("Setting up Jodel account...") while True: try: account = jodel_api.JodelAccount(lat=lat, lng=lng, city=city) print("Done.") break except: if y_n("Something went wrong. Retry?"): pass else: sys.exit() # Try to verify account print("Verifying account...") if y_n("Do you have an android account? (android_id and security_token)?\nThis will increase the success chance of the verification."): android_id = int(input("android_id:\n")) security_token = int(input("security_token:\n")) a = jodel_api.AndroidAccount(android_id, security_token) else: a = jodel_api.AndroidAccount() response = account.verify(a) while not response[0] == 200: if y_n("Couldn't verify account.\nServer response was: {0}\nRetry?".format(response)): response = account.verify(a) else: sys.exit() print("Done.") return JodelAcc(lat, lng, city, account.access_token, account.expiration_date, account.refresh_token, account.distinct_id, account.device_uid, account.is_legacy)
#Read Tokent try: f = open('token', 'r') dict_accountdata = eval(f.read()) f.close() print(dict_accountdata) except: print("token file not found..skipping authentification") #Login try: j = jodel_api.JodelAccount(lat=lat, lng=lng, city=city, access_token='XXX', expiration_date='1518130677', refresh_token='XXX', distinct_id='5a739b75633fde0016eb80d1', device_uid='bd1986a13456033f57d0', is_legacy=True) #TODO add something (privacy) accountvalid = True except: print("Jodel account not valid anymore.") accountvalid = False #Recreate account if accountvalid == False: print("Create jodel account.") j = jodel_api.JodelAccount(lat=lat, lng=lng, city=city) dict_accountdata = j.refresh_access_token() #Create Token
from time import sleep import jodel_api import hmac, hashlib from jodel_api import gcmhack if __name__ == '__main__': lat, lng, city = 48.148434, 11.567867, "Munich" account = gcmhack.AndroidAccount() sleep(5) token = account.get_push_token() j = jodel_api.JodelAccount(lat=lat, lng=lng, city=city, pushtoken=token) print('Account created') print('Verification: {}'.format(j.verify(android_account=account))) print(j.get_posts_popular()) print('Karma is: {}'.format(j.get_karma())) print(j.upvote('5d238be544a6a0001a0360d6')) print('Karma is: {}'.format(j.get_karma())) #req = 'POST%api.go-tellm.com%443%/api/v2/users/%%%2019-01-10T21:11:59Z%%{"location":{"country":"DE","city":"Heilbronn","loc_coordinates":{"lng":9.2070918,"lat":49.1208046},"loc_accuracy":16.581},"registration_data":{"channel":"","provider":"branch.io","campaign":"","feature":"","referrer_branch_id":"","referrer_id":""},"client_id":"81e8a76e-1e02-4d17-9ba0-8a7020261b26","device_uid":"573b2e648c7b3849f1a533167354f4752f0466010c72bdde378a5d856421b122","language":"de-DE"}' #signature = hmac.new('TNHfHCaBjTvtrjEFsAFQyrHapTHdKbJVcraxnTzd'.encode("utf-8"), req.encode("utf-8"), hashlib.sha1).hexdigest().upper() #print(signature)
# Simultanious processing of the post strings. queue1 = Queue() queue2 = Queue() Process(target = getPostData, args = (queue1, data.API_KEY, data.CITY)).start() Process(target = getPollenPostData, args = (queue2, data.pollen_region, data.pollen_partregion)).start() PostData = queue1.get() PollenPostData = queue2.get() # Initializes the Jodel account. try: account = jodel_api.JodelAccount( lat=data.lat, lng=data.lng, city=data.city, access_token=data.access_token, expiration_date=data.expiration_date, refresh_token=data.refresh_token, distinct_id=data.distinct_id, device_uid=data.device_uid, is_legacy=data.legacy) except Exception as Ex: if Ex.args[0] == "Error updating location: (401, 'Unauthorized')": account = refresh_all(data, args.account) else: raise Ex else: # Refresh access using refresh_access(), which also writes the new tokens back to the account file. # But only if the access_token has expired or will expire within the next 5 minutes. epoch_time = int(time.time()) - 300 if epoch_time > data.expiration_date: refresh_access(account, data, args.account)